扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:袁晓辉编译 来源:blog 2007年11月11日
关键字:
class Universe { static void WorkerStartedWork() { Console.WriteLine("Universe notices worker starting work"); } static int WorkerCompletedWork() { Console.WriteLine("Universe pleased with worker's work"); return 7; } static void Main() { Worker peter = new Worker(); Boss boss = new Boss(); peter.completed = new WorkCompleted(boss.WorkCompleted); peter.started = new WorkStarted(Universe.WorkerStartedWork); peter.completed = new WorkCompleted(Universe.WorkerCompletedWork); peter.DoWork(); Console.WriteLine(“Main: 工人工作完成”); Console.ReadLine(); } } |
// Peter's boss taking matters into his own hands if( peter.completed != null ) peter.completed(); |
class Worker { ... public event WorkStarted started; public event WorkProgressing progressing; public event WorkCompleted completed; } |
static void Main() { Worker peter = new Worker(); Boss boss = new Boss(); peter.completed += new WorkCompleted(boss.WorkCompleted); peter.started += new WorkStarted(Universe.WorkerStartedWork); peter.completed += new WorkCompleted(Universe.WorkerCompletedWork); peter.DoWork(); Console.WriteLine(“Main: 工人工作完成”); Console.ReadLine(); } |
public void DoWork() { ... Console.WriteLine("“工作: 工作完成”"); if( completed != null ) { foreach( WorkCompleted wc in completed.GetInvocationList() ) { int grade = wc(); Console.WriteLine(“工人的工作得分=” + grade); } } } |
class Boss { public int WorkCompleted() { System.Threading.Thread.Sleep(3000); Console.WriteLine("Better..."); return 6; /* 总分为10 */ } } class Universe { static int WorkerCompletedWork() { System.Threading.Thread.Sleep(4000); Console.WriteLine("Universe is pleased with worker's work"); return 7; } ... } |
public void DoWork() { ... Console.WriteLine("“工作: 工作完成”"); if( completed != null ) { foreach( WorkCompleted wc in completed.GetInvocationList() ) { wc.BeginInvoke(null, null); } } } |
public void DoWork() { ... Console.WriteLine("“工作: 工作完成”"); if( completed != null ) { foreach( WorkCompleted wc in completed.GetInvocationList() ) { IAsyncResult res = wc.BeginInvoke(null, null); while( !res.IsCompleted ) System.Threading.Thread.Sleep(1); int grade = wc.EndInvoke(res); Console.WriteLine(“工人的工作得分=” + grade); } } } |
public void DoWork() { ... Console.WriteLine("“工作: 工作完成”"); if( completed != null ) { foreach( WorkCompleted wc in completed.GetInvocationList() ) { wc.BeginInvoke(new AsyncCallback(WorkGraded), wc); } } } private void WorkGraded(IAsyncResult res) { WorkCompleted wc = (WorkCompleted)res.AsyncState; int grade = wc.EndInvoke(res); Console.WriteLine(“工人的工作得分=” + grade); } |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者