委托的用法

案例一:

//声明一个委托

delegate void DEAddListPhone(string txt);

//要调用的方法执行的逻辑
private void AddListNo(string txt)
{
txtNotOpen.Text += txt + " ";
}

//需要的时候直接调用

 this.Invoke(new DEAddListPhone(AddListNo), new object[] { phone });

案例二:

//委托
delegate void hLogNote(string title, string content);
//更新普通日志
private void LogNote(string title, string content)
{
int j = 200;
if (this.ltbMessage.Items.Count > j)
{
try
{
this.ltbMessage.Items.Remove(this.ltbMessage.Items[j]);
}
catch (Exception ex) { }
}
this.ltbMessage.Items.Insert(0, GetDateTime() + " " + "[" + title + "] " + content);
}

//调用

 this.Invoke(new hLogNote(LogNote), new object[] { "系统", provinceName + "-" + areaName + "加载完毕!" });

原文地址:https://www.cnblogs.com/lacey/p/5555526.html