static void Main(string[] args) { Console.WriteLine("主线程." + Thread.CurrentThread.ManagedThreadId); FooDelegate fooDelegate = Foo; fooDelegate.BeginInvoke("Hello world.", FooComepleteCallback, fooDelegate); Console.WriteLine("主线程继续执行..." + Thread.CurrentThread.ManagedThreadId); Console.WriteLine("Press any key to continue..."); Console.ReadKey(true); }
static void Main(string[] args) { Console.WriteLine("主线程" + Thread.CurrentThread.ManagedThreadId); FooDelegate fooDelegate = Foo; IAsyncResult result = fooDelegate.BeginInvoke("Hello World.", null, null); Console.WriteLine("主线程继续执行..."); //当使用BeginInvoke异步调用方法时,如果方法未执行完,EndInvoke方法就会一直阻塞,直到被调用的方法执行完毕 int n = fooDelegate.EndInvoke(result); Console.WriteLine("回到主线程" + Thread.CurrentThread.ManagedThreadId); Console.WriteLine("结果是" + n); Console.ReadKey(true); }