110,567
社区成员
发帖
与我相关
我的任务
分享
delegate void WorkStarted();
delegate void WorkProgressing();
delegate void WorkCompleted();
public void DoWork()
{
...
Console.WriteLine("Worker:work completed");
if ( completed !=null )
{
foreach( WorkCompleted wc in completed.GetInvocationList() )
wc.BeginInvoke( new AsyncCallback(WorkGraded),wc );
}
}
void WorkGraded (IAsyncResult res )
{
WorkCompleted wc = (WorkCompleted)res.AsyncState;
int grade = wc.EndInvoke(res);
Console.WriteLine("Worker grade= "+grade);
}
上面的代码是从Windows Forms 程序设计中抄的,你看看其中的附录,讲的是委托编程,说的会更细致。
//调用事件,如果客户端异常断开,则自动去掉相应的回调
if (NewHitchEvent != null)
{
hitch.TID = iid;
hitch = GetHitch(hitch);
HitchEventHandler tempevent = null;
foreach (Delegate del in NewHitchEvent.GetInvocationList())//单独处理事件的委托链中的每一个委托
{
try
{
tempevent = (HitchEventHandler)del;
tempevent(hitch);//调用此委托,触发客户端事件
}
catch
{
NewHitchEvent -= tempevent;//因为超时异常(或客户端断网),调用失败,所以可以假定客户端已失去联系,直接去掉此委托,下次再次触发事件时便不再对此客户端的回调函数调用
}
}
}