码迷,mamicode.com
首页 > 编程语言 > 详细

多线程

时间:2017-01-15 12:43:37      阅读:341      评论:0      收藏:0      [点我收藏+]

标签:array   多线程   GridView   sage   eth   show   信号量控制   ack   stop   

信号量控制线程池

        //信号量
        Semaphore semahoro;
        private void button1_Click(object sender, EventArgs e)
        {
            Thread t = new Thread(new ThreadStart(ThreadPooltemp));
            t.Start();
        }
        private void ThreadPooltemp()
        {
            semahoro = new Semaphore(3, 3);
            var ehs = new List<ManualResetEvent>();
            for (int i = 0; i < 10; i++)
            {
                var m = new ManualResetEvent(false);
                ehs.Add(m);
                ThreadHelper th = new ThreadHelper();
                th.id = i;

                ThreadPool.QueueUserWorkItem(h =>
                {
                    semtemp(th);
                    m.Set();
                });
            }
            //第一种方式等待
            WaitHandle.WaitAll(ehs.ToArray());
            //第二种方式等待
            foreach (var m in ehs)
            {
                m.WaitOne();
            }
            MessageBox.Show("ThreadPool >>> ok!");
        }
        public void semtemp(ThreadHelper thh)
        {
            semahoro.WaitOne();
            Run(thh);
            semahoro.Release(1);
        }
        private void Run(ThreadHelper mny)
        {

            this.Invoke(new EventHandler(delegate
             {

                 textBox1.AppendText(mny.id.ToString() + "\r\n");

             }));
            Thread.Sleep(2000);
            //第二种委托
            //this.Invoke(new MethodInvoker(delegate
            //{
            //    dataGridView1.Rows[row].Cells[cll].Value = con;
            //}));
        }

 信号量构建多线程

 

       //信号量
        Semaphore semahoro;
        private void button2_Click(object sender, EventArgs e)
        {
            Thread th = new Thread(new ThreadStart(delegate
            {
                sem();
            }));
            th.IsBackground = true;
            th.Start();
        }
        public void sem()
        {
            semahoro = new Semaphore(3, 3);
            var ehs = new List<ManualResetEvent>();
            for (int i = 0; i < 10; i++)
            {
                var m = new ManualResetEvent(false);
                ehs.Add(m);
                ThreadHelper thh = new ThreadHelper();
                thh.id = i;
                Thread th = new Thread(new ThreadStart(delegate
                {
                    semtemp(thh);
                    m.Set();
                }));
                th.IsBackground = true;
                th.Start();
            }
            //第一种方式等待
            WaitHandle.WaitAll(ehs.ToArray());
            //第二种方式等待
            foreach (var m in ehs)
            {
                m.WaitOne();
            }
            this.Invoke(new MethodInvoker(delegate
            {
                MessageBox.Show(this, "Semaphore >>> ok!");
            }));
        }
        public void semtemp(ThreadHelper thh)
        {
            semahoro.WaitOne();
            Run(thh);
            semahoro.Release(1);
        }
        private void Run(ThreadHelper mny)
        {

            this.Invoke(new EventHandler(delegate
            {

                textBox1.AppendText(mny.id.ToString() + "\r\n");

            }));
            Thread.Sleep(2000);
            //第二种委托
            //this.Invoke(new MethodInvoker(delegate
            //{
            //    dataGridView1.Rows[row].Cells[cll].Value = con;
            //}));
        }

也可以把线程全部加到List集合

//投递完毕,等待线程完成!
for (int i = 0; i < ths.Count; i++)
{
while (ths[i].ThreadState != System.Threading.ThreadState.Stopped)
{
Application.DoEvents();
}
}

多线程

标签:array   多线程   GridView   sage   eth   show   信号量控制   ack   stop   

原文地址:http://www.cnblogs.com/milest/p/6286857.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!