您的位置:首页 > 移动开发 > Objective-C

C#中Join()方法实现线程的顺序执行

2006-05-19 15:21 387 查看
在多线程中,某些线程的执行需要建立在另一个线程完成的基础上的,通过ThreadPool可以完成这样功能,但我 不会^_^,查了若干资料也没有弄清楚怎么做。后来发现线程中的Join()方法也可以实现同样的功能。

程序代码如下:

using System.Thread;

        private System.Windows.Forms.RichTextBox richTextBox1;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Button button2;
        private bool down = false;
        Private Thread th1;
        Private Thread th2;  

private void UpdateStr(string str)
        {
            richTextBox1.AppendText(str + "/r/n");
        }

        private void UpdateString1()
        {
            UpdateStr("Thread1 is runnning!");
        }

        private void Updatestring2()
        {
            UpdateStr("Thread2 is running!");
        }

        private void thread1()
        {
                     while (!down)
            {
                MethodInvoker mi = new MethodInvoker(UpdateString1);
                this.Invoke(mi);
                Thread.Sleep(1000);
            }           
                   }

        private void thread2()
        {
            th1.Join();
            while (down)
            {
                MethodInvoker mi = new MethodInvoker(Updatestring2);
                this.Invoke(mi);
                Thread.Sleep(1000);
            }            
       }

private void button1_Click(object sender, EventArgs e)
        {
            th1 = new Thread(new ThreadStart(thread1));
            th1.Start();
            th2 = new Thread(new ThreadStart(thread2));
            th2.Start();
        }

 private void button2_Click(object sender, EventArgs e)
        {
            down = !down;
        }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息