using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;namespace threads_test
{

    delegate void SetThreadTest();
    public partial class Form1 : Form
    {
        static int var=0;
        Thread test;
        public Form1()
        {
            InitializeComponent();
        }
        void test_fun()
        {
            if(this.RichTextBox1.InvokeRequired)
        {
        SetThreadTest d=new SetThreadTest(test_fun);
                this.Invoke(d, new object[] { });
            }
        else
        {
                for (int i = 1; i <=8*var; i++)
                {
                    if (i % var == 0)
                    { 
                        RichTextBox1.Text += "the thread's name is " + Thread.CurrentThread.Name + "\n";
                        RichTextBox1.Text += i.ToString() + "\n";
                    }
                }
            }  
        }        private void button1_Click(object sender, EventArgs e)
        {
            var = Int32.Parse(textBox1.Text);
            ThreadStart Ts = new ThreadStart(test_fun);
            test = new Thread(Ts);
            test.Name = "test";
            test.Priority = ThreadPriority.Highest;
            test.Start();
            test_fun();
        }
    }
}有代码如上,赋值静态变量var为1000000,期待在RichTextBox1中得到以下结果:
the thread's name is test
1000000
the thread's name is test
2000000
the thread's name is test
3000000
the thread's name is test
4000000
the thread's name is test
5000000
the thread's name is test
6000000
the thread's name is test
7000000
the thread's name is test
8000000
the thread's name is 
1000000
the thread's name is 
2000000
the thread's name is 
3000000
the thread's name is 
4000000
the thread's name is 
5000000
the thread's name is 
6000000
the thread's name is 
7000000
the thread's name is 
8000000但程序实际输出为:
the thread's name is 
1000000
the thread's name is 
2000000
the thread's name is 
3000000
the thread's name is 
4000000
the thread's name is 
5000000
the thread's name is 
6000000
the thread's name is 
7000000
the thread's name is 
8000000
the thread's name is 
1000000
the thread's name is 
2000000
the thread's name is 
3000000
the thread's name is 
4000000
the thread's name is 
5000000
the thread's name is 
6000000
the thread's name is 
7000000
the thread's name is 
8000000
问题如下:
1.线程同委托之间存在什么关系?2.为何test线程在运行时,输出Thread.CurrentThread.Name的值不是我所赋予的(test.Name = "test";)的test,而是为空.3.SetThreadTest d=new SetThreadTest(test_fun);
  this.Invoke(d, new object[] { });这两句的意义是什么呢?第一次发贴求问,请大家能帮帮,谢谢了.

解决方案 »

  1.   

    invoke和委托防止uI死锁,这个例子是。net2推荐的写法。
      

  2.   

    whycom..我知道不是同一个Thread的...所以我期待能得到the thread 's name is test 这样的结果.当实际上的结果是我贴出的第二个.
      

  3.   

    是这样的
     if(this.RichTextBox1.InvokeRequired)  // 第一次调用inverkerquired 是false 引起了以下调用
            { 
            SetThreadTest d=new SetThreadTest(test_fun); 
                    this.Invoke(d, new object[] { }); //再一次调用了 test_fun() 但不是同一个线程
                } 
            else 
      

  4.   

    sorry
    some error 
    false ==>true
      

  5.   

    说说个人认为的
     test_fun(); 这个没啥说的结果肯定是 test.Start(); 
    这个调用后
    SetThreadTest d=new SetThreadTest(test_fun);  
    this.Invoke(d, new object[] { });  走的是这个
    既然走了委托 他的线程名字肯定就没了既然用了Invoke 就应该不会出现资源征用的情况
    所以执行的顺序是 
    test.Start(); 这个线程, 然后走this.Invoke(d, new object[] { });  
    在test_fun(); 的循环
    最后是invoke调用的test_fun(); 的循环
      

  6.   

    谢谢上边的两位...whycom很热情...
    虽然cancerser回答得很好理解.但也只回答了第二个问题.
    期待1,3有人解决...我马上就结帖啦..cancerser也都说:都是混饭吃,记得要结帖
    哈哈...
      

  7.   

    1.线程同委托之间存在什么关系? 
    ===================================
      没有关系,不过一般线程都是函数,而委托就是函数指针,所以线程里经常会用到委托,例如threadstart, 还有同步调用 invoke 异步调用 begininvoke 都是通过委托作的
     
    3.SetThreadTest d=new SetThreadTest(test_fun); 
      this.Invoke(d, new object[] { });这两句的意义是什么呢? 
    ==================================================
     建立一个委托,上面说过 委托是函数指针,所以 d 就是指向test_fun的函数指针
      this.invoke ,this 指的是你的form  invoke 同步调用
      

  8.   

    谢谢whycom...
    结贴给分了!!!