public class Program { private static void heavyMethod(int pauseTime) { Thread.Sleep(pauseTime); int threadID = Thread.CurrentThread.ManagedThreadId; Console.WriteLine(string.Format("My runtime was {0} miliseconds, my id was {1}",
pauseTime.ToString(), threadID));
} private delegate void myDelegate(int iCallTime); private static myDelegate dlgt = new myDelegate(heavyMethod); private static void Main(string[] args) { Console.WriteLine("Start demo"); //simple async call, go do your stuff and i don't care when you finish dlgt.BeginInvoke(10000, null, null); //Async call with call back function, when compelte call fourSecond_callback method dlgt.BeginInvoke(4000, new AsyncCallback(fourSecond_CallBack), null); //async call with result, waits for end invoke Console.WriteLine("Waiting for 7s"); IAsyncResult ar1 = dlgt.BeginInvoke(7000, null, null); dlgt.EndInvoke(ar1); //asyn call with wait IAsyncResult ar = dlgt.BeginInvoke(3000, null, null); Console.WriteLine("Doing some stuff here"); ar.AsyncWaitHandle.WaitOne(); //async with polling Console.WriteLine("5s counter start"); IAsyncResult ar2 = dlgt.BeginInvoke(5000, null, null); int count = 0; while (!ar2.IsCompleted) { Console.WriteLine(count++); Thread.Sleep(1000); } Console.WriteLine("Finished, press any key"); Console.ReadKey(); } public static void fourSecond_CallBack(IAsyncResult ar) { Console.WriteLine("4s is up"); } }