c#后台线程访问前台控件并显示信息示例
发布时间:2016-12-28 来源:查字典编辑
摘要:复制代码代码如下://设置为后台线程Threadth=newThread(delegate(){append();});th.IsBackg...
复制代码 代码如下:
//设置为后台线程
Thread th = new Thread(delegate() {
append();
});
th.IsBackground = true;
th.Start();
//在append方法里面需要调用前台控件
public void append(){
// ... 业务处理
this.Invoke(new flushMessage(showMessage), new object[] { row["Code"].ToString(), res });
}
//委托flushMessage和方法showMessage签名必须一致
private delegate void flushMessage(String id, String res);
private void showMessage(String id,String res)
{
if (res == "true")
{
txtMsg.Text += "tttt" + id + "tttt导入成功rn";
}
else
{
txtMsg.Text += "tttt" + id + "ttttt导入失败rn";
}
}