在winform编程中常遇到此类问题,造成辅助线程无法给控件赋值

 

 1 //定义委托
 2 
 3 private delegate void SetTextCallback(string text);
 4 
 5  
 6 
 7 //在给textBox1.text赋值的地方调用以下方法即可
 8 
 9 private void SetText(string text)
10         {
11             // InvokeRequired需要比较调用线程ID和创建线程ID
12             // 如果它们不相同则返回true
13             if (this.textBox1.InvokeRequired)
14             {
15                 SetTextCallback d = new SetTextCallback(SetText);
16                 this.Invoke(d, new object[] { text });
17             }
18             else
19             {
20                 this.textBox1.Text = text;
21             }
22         }

 

相关文章:

  • 2021-08-28
  • 2022-02-10
  • 2022-01-08
猜你喜欢
  • 2021-11-28
  • 2022-12-23
  • 2021-11-28
  • 2021-11-28
  • 2021-11-28
  • 2021-11-28
  • 2021-11-28
相关资源
相似解决方案