c#中如何跨线程调用windows控件

来源:百度知道 编辑:UC知道 时间:2024/05/14 17:25:06

在辅助线程调用(可通过delegate)下面的方法InvokeControl();

//写在主线程中(控件)
private void InvokeControl()
{
if (this.InvokeRequired)
this.Invoke(new DelegateChangeText(ChangeText));
else
this.ChangeText();
}

private void ChangeText()
{
this.TextBox.Text = "sd";
}

public delegate void DelegateChangeText();

在构造函数里使用属性
CheckForIllegalCrossThreadCalls 值设置为 false
即 public Form1()
{
CheckForIllegalCrossThreadCalls = false;
}