Class Program {private delegate int MyDel (int a); public static int MyMenthod (int a) {for (int I = 0; I <1000; I ++) {a ++;} return a;} static void Main (string [] args) {var del = new MyDel (MyMenthod); Console. writeLine ("Before BeginInvoke"); IAsyncResult iar = del. beginInvoke (5, null, null); Console. writeLine ("After BeginInvoke"); Console. writeLine ("Doing stuff"); long result = del. endInvoke (iar); Console. writeLine ("result: {0}", result );}}
Class Program {private delegate int MyDel (int a); public static int MyMenthod (int a) {for (int I = 0; I <1000; I ++) {a ++;} Thread. sleep (3000); return a;} static void Main (string [] args) {var del = new MyDel (MyMenthod); IAsyncResult iar = del. beginInvoke (5, null, null); Console. writeLine ("After BeginInvoke"); while (! Iar. isCompleted) {Console. writeLine ("not finished"); Thread. sleep (1, 2000); Console. writeLine ("continue to process other things");} Console. writeLine ("Asynchronous execution completed"); long result = del. endInvoke (iar); Console. writeLine ("result: {0}", result );}}
Class Program {private delegate int MyDel (int a); public static int MyMenthod (int a) {for (int I = 0; I <1000; I ++) {a ++;} return a;} public static void CallBack (IAsyncResult iar) {AsyncResult ar = iar as AsyncResult; MyDel del = (MyDel) ar. asyncDelegate; long r = del. endInvoke (iar); Thread. sleep (1, 2000); Console. writeLine ("the result is: {0}", r);} static void Main (string [] args) {var del = new MyDel (MyMenthod); Console. writeLine ("After BeginInvoke"); IAsyncResult iar = del. beginInvoke (5, CallBack, del); Console. writeLine ("Doing More Work In Main"); Thread. sleep (5000 );}}