public DicomState EndPrint(IAsyncResult ar) { PrintDelegate asyncDelegate = ((AsyncResult)ar).AsyncDelegate as PrintDelegate; if (asyncDelegate == null) { throw new InvalidOperationException("cannot get results, asynchresult is null"); } return(asyncDelegate.EndInvoke(ar)); }
static void Case1() { Console.WriteLine("main thread is started."); PrintDelegate print = new PrintDelegate(Print); IAsyncResult result = print.BeginInvoke("hello world.", null, null); Console.WriteLine("main thread is going."); print.EndInvoke(result);//EndInvode()负责一直阻塞当前线程,等待返回结果后继续执行 Console.WriteLine("compeleted."); }
public static void Test() { PrintDelegate printDelegate = Print; Console.WriteLine("主线程"); IAsyncResult result = printDelegate.BeginInvoke("Hello World.", null, null); Console.WriteLine("主线程继续执行..."); //当使用BeginInvoke异步调用方法时,如果方法未执行完, //EndInvoke方法就会一直阻塞,直到被调用的方法执行完毕 printDelegate.EndInvoke(result); Console.WriteLine("Press any key to continue..."); Console.ReadKey(true); }