近期写了几个C#客户端软件,其中,上传数据时另开线程,同时要更新UI,跨线程操作,用到委托。用法如下:
//委托
public delegate void delegate1(string str);
//方法
private void showUpdateProgress(string content)
{
//...
rtbxUploadLog.Text += content;
}
//调用
delegate1 change = new delegate1(showUpdateProgress);
string resp = upload();
if (resp.Equals("ok"))
{
this.BeginInvoke(change, "上传完成");
}else{
this.BeginInvoke(change, "上传失败");
}
委托的概念,刚接触有些难以理解,多加练习就能熟悉了。