1.委托
//定义全局变量声明委托
public delegate void deleshowcode(Code code); //可带参数
//定义函数体
public void showcode(Code code)
{
if (this.InvokeRequired)
{
this.Invoke(new deleShowCode(showcode), code);
}
else
{
code.ShowDialog(this); //实现的语句 可随意编写
}
}
2.线程同步
//声明全局线程同步
public readonly SynchronizationContext m_SyncContext = null;
//实例化线程同步
m_SyncContext = SynchronizationContext.Current;
//实现函数
public void setTextval(string s)
{
m_SyncContext.Post(setT, s);
}
public void setT(object s)
{
Label1.Text = s.ToString();
}
3.Invoke
//在子线程内直接使用 即可更新UI
Invoke(new Action(() =>{更新语句}));