http://blog.csdn.net/baple/article/details/21524505 这个很实用。
声明委托
delegate void SetLabelTextDelegate(string str);
private void SetLabelText(string str)
{
if (ArchiveInfo.InvokeRequired)
{
Invoke(new SetLabelTextDelegate(SetLabelText), new string[] { str });
}
else
{
ArchiveInfo.AppendText(str);
}
}
delegate void SetButtonEnableDelegate(bool bRet);
private void SetButtonEnable(bool bRet)
{
if (StartArchive.InvokeRequired)
{
Invoke(new SetButtonEnableDelegate(SetButtonEnable), bRet);
}
else
{
StartArchive.Enabled = bRet;
}
}
线程方法
public void UpdateUI(object ti)
{
SetLabelText("xx1");
SetButtonEnable(true);
}
线程调用:
Thread updateUIThread = new Thread(new ParameterizedThreadStart(UpdateUI));
updateUIThread.Start(TotalImageStatus);
------------------------------------------------------------------------------------------------------------
另外一种方法(不用代理):
Thread waitForCGet = new Thread(new ParameterizedThreadStart(WaitDownloadImage));
waitForCGet.IsBackground = true;
waitForCGet.Start(dpList);
private void WaitDownloadImage(object obj)
{
this.CGetAll.Enabled = true;
this.CFindStop.Enabled = false;
this.CFindAll.Enabled = true;
}