多线程同时操作界面使用互斥体

private void button4_Click(object sender, EventArgs e)
        {
            Mutex m = new Mutex();
            autoevent = new AutoResetEvent(false);

            Thread t = new Thread(delegate()
            {
                Thread.Sleep(500);
                for (int i = 0; i < 50000; i++)
                {
                    m.WaitOne();
                    try
                    {
                        label1.Invoke
                        (
                            (MethodInvoker)
                            delegate
                            {
                                label1.Text = i.ToString();
                            }
                        );
                    }
                    finally
                    {
                        m.ReleaseMutex();
                    }
                }
            });
            t.Start();

            Thread t2 = new Thread(delegate()
            {
                for (int i = 0; i < 50000; i++)
                {
                    m.WaitOne();
                    try
                    {
                        label1.Invoke((System.Action)delegate()
                        {

                            label1.Text = i.ToString();

                        });
                    }
                    finally
                    {
                        m.ReleaseMutex();
                    }

                }
            });
            t2.Start();
        }
posted @ 2013-06-18 15:36  perock  阅读(747)  评论(1编辑  收藏  举报