winform跨线程
在winform中经常用到多线程,那么用多线程对控件进行赋值和修改的时候呢会出现类似“该控件不是当前线程创建”的错误信息,在winform中两种办法:
1.在加载事件中写这句话,其作用呢就是线程的异步调用
1 System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
2.用委托来实现线程对控件的赋值操作
1 public delegate void OutDelegate(string text); 2 public void OutText(string text) 3 { 4 txt.AppendText(text); 5 txt.AppendText( "\t\n" ); 6 } 7 OutDelegate outdelegate = new OutDelegate( OutText ); 8 this.BeginInvoke(outdelegate, new object[]{text});
补充一句:
在winform中创建带参数的线程,可以这么写
1 Thread thread = new Thread(new ParameterizedThreadStart(getweathering)); 2 3 thread.Start(comboBox4.Text); 4 5 6 7 void getweathering(string temptext) 8 9 {}
本文来自博客园,作者:童心少年,转载请注明原文链接:https://www.cnblogs.com/makeblock/p/4243795.html