首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

多线程访问窗体控件失误

2011-12-10 
多线程访问窗体控件出错哪位老师教我!谢谢!delegatevoidSetTextCallback1(stringtest)publicvoidTestClas

多线程访问窗体控件出错
哪位老师教我!谢谢!            


delegate   void   SetTextCallback1(string   test);
                public   void   TestClass(string   test)
                {
                        if   (this.textBox2.InvokeRequired)
                        {
                                SetTextCallback1   a   =   new   SetTextCallback1(TestClass);
                                this.Invoke(a,   new   object[]   {   test   });
                        }
                        else
                        {
                                for   (int   i   =   0;   i   <   100;   i++)
                                {
                                        this.textBox2.Text   +=test+   "\r\n ";    
                                }
                               
                        }
                }
                public   void   TestClass1()
                {
                        if   (this.textBox1.InvokeRequired)
                        {
                                SetTextCallback   d   =   new   SetTextCallback(TestClass1);
                                this.Invoke(d,   new   object[]   {});
                        }
                        else
                        {
                                for   (int   i   =   0;   i   <   500;   i++)
                                {
                                        this.textBox1.Text   +=   DateTime.Now.ToString( "yyyy年MM月dd日 ")   +   "\r\n ";


                                }
                        }
                }
                private   void   Form1_Load(object   sender,   EventArgs   e)
                {

                }

                private   void   button1_Click(object   sender,   EventArgs   e)
                {
                        Thread   thd1   =   new   Thread(new   ThreadStart(TestClass));//在这里出错,“TestClass”的重载均与委托“System.Threading.ThreadStart”不匹配

                        Thread   thd2   =   new   Thread(new   ThreadStart(TestClass1));
                        thd1.Start();
                        thd2.Start();

                }

[解决办法]
delegate void SetTextCallback1(string test);
public void TestClass(string test)
{
if (this.textBox2.InvokeRequired)
{
SetTextCallback1 a = new SetTextCallback1(TestClass);
this.Invoke(a, new object[] { test });
}
else
{
for (int i = 0; i < 100; i++)
{
this.textBox2.Text += test + "\r\n ";
}
}
}
private void button1_Click(object sender, EventArgs e)
{
Thread thd1 = new Thread(new ThreadStart(ProcThreadone));
thd1.Start();
}
private void ProcThreadone()
{
TestClass( "Test Thread ");
}

热点排行