using System; using System.Threading; publicdelegateint AddHandler(int a, int b); publicclass Foo { staticvoid Main() { Console.WriteLine("**********SyncInvokeTest**************"); AddHandler handler =new AddHandler(Add); int result = handler.Invoke(1,2); Console.WriteLine("Do other work ... ... ..."); Console.WriteLine(result); Console.ReadLine(); } staticint Add(int a, int b) { Console.WriteLine("Computing "+a+" + "+b+" ..."); Thread.Sleep(3000); Console.WriteLine("Computing Complete."); return a+b; } }
运行结果:
**********SyncInvokeTest************** Computing 1 + 2 ... Computing Complete. Do other work... ... ... 3