System.Threading.Thread th;
public delegate void SetValueEventHandler(Label label, string text);
private void btnUpdate_Click(object sender, EventArgs e)
{
th = new System.Threading.Thread(new System.Threading.ThreadStart(do1));
th.Start();
}
private void SetValueEvent(Label label, string text)
{
label.Text = text;
}
public void do1()
{
SetValueEventHandler setValue = new SetValueEventHandler( SetValueEvent );
for( int i =0; i < 100000; i++ )
{
Invoke(setValue,new object[]{label1,i.ToString()} );
}
}
还有一种更简单的就是在线程开始前
Control.CheckForIllegalCrossThreadCalls = false;
委托就可以完全不要了
/// <summary>
/// 利用委托处理
/// </summary>
/// <param name="btn"></param>
/// <param name="b"></param>
private delegate void SetValueEventHandler(Button btn, bool b);
private void SetValuesEvent(Button btn,bool b)
{
btn.Enabled = b;
}
下面是线程启动时加入的
//this.btnLoginOk.Enabled = true;
SetValueEventHandler setValue = new SetValueEventHandler(SetValuesEvent);
Invoke(setValue, new object[] {this.btnLoginOk,true });