.NET delegate 委托的使用

以下一般用在线程中更新UI

public delegate void MyInvoke(string str);//有参数

private void ListDelete(string deletestr)
{
if (this.InvokeRequired)
{
MyInvoke _myInvoke = new MyInvoke(ListDelete);
this.Invoke(_myInvoke, new object[] { deletestr });
}
else
{}
}
public delegate void MyInvokenoarg();//无参数

private void ClearNowSendToList()
{
if (this.InvokeRequired)//等待异步
{
MyInvokenoarg _myInvoke = new MyInvokenoarg(ClearNowSendToList);
this.Invoke(_myInvoke, new object[] { });
}
else
{ }
}

类中委托,供外部调用

类:

namespace GoogleSearchCollector
{
public class GoogleResultCollectClass
{

public delegate void SearchLogHandler(string msg,int msgtype);
public event SearchLogHandler SearchLog;

}

}

调用:

GoogleResultCollectClass google = new GoogleResultCollectClass(txtSearchCommand.Text,ref webBrowser1);
google.SearchLog += new GoogleResultCollectClass.SearchLogHandler(SearchLog);

private void SearchLog(string msg, int msgtype)
{
switch (msgtype)
{
case 1://URL
txtURLList.AppendText(msg + "\r\n");
txtURLList.SelectionStart = txtURLList.Text.Length;
txtURLList.ScrollToCaret();
break;
case 2://status
toolStripStatusWebBrowser.Text = msg;
break;
case 3://Total
toolStripStatusTotal.Text = msg;
break;
}
}


就地使用的委托

this.dgwTagEPC.Invoke(new MethodInvoker(delegate
{

txtName="一叶知秋     yyzq.net      tczhuoyou.com";
}

你可能感兴趣的:(.net,String,object,Google,Class,WebBrowser)