static void Main(string[] args) { // assign the deleate PerformCalc myDelegate = PerformCalcMethod; // call the method several times myDelegate.BeginInvoke(0, int.MaxValue, 1, AsyncMethodCallback, myDelegate); myDelegate.BeginInvoke(0, int.MaxValue / 2, 1, AsyncMethodCallback, myDelegate); myDelegate.BeginInvoke(0, int.MaxValue / 4, 4, AsyncMethodCallback, myDelegate); Console.WriteLine("Async methods are running..."); // wait for input before exiting Console.WriteLine("Press enter to finish"); Console.ReadLine(); }
static void Main(string[] args) { // assign the deleate PerformCalc myDelegate = PerformCalcMethod; // call the method several times IAsyncResult res1 = myDelegate.BeginInvoke(0, int.MaxValue, 1, AsyncMethodCallback, myDelegate); IAsyncResult res2 = myDelegate.BeginInvoke(0, int.MaxValue / 2, 1, AsyncMethodCallback, myDelegate); IAsyncResult res3 = myDelegate.BeginInvoke(0, int.MaxValue / 4, 4, AsyncMethodCallback, myDelegate); Console.WriteLine("Async methods are running..."); // wait for each of the async methods to complete res1.AsyncWaitHandle.WaitOne(); res2.AsyncWaitHandle.WaitOne(); res3.AsyncWaitHandle.WaitOne(); Console.WriteLine("Async methods have all completed"); // wait for input before exiting Console.WriteLine("Press enter to finish"); Console.ReadLine(); }
static void Main(string[] args) { // assign the deleate PerformCalc myDelegate = PerformCalcMethod; // call the method several times IAsyncResult res1 = myDelegate.BeginInvoke(0, int.MaxValue, 1, null, myDelegate); IAsyncResult res2 = myDelegate.BeginInvoke(0, int.MaxValue / 2, 1, null, myDelegate); IAsyncResult res3 = myDelegate.BeginInvoke(0, int.MaxValue / 4, 4, null, myDelegate); Console.WriteLine("Async methods are running..."); foreach (IAsyncResult res in new IAsyncResult[] { res1, res2, res3 }) { long result = myDelegate.EndInvoke(res); Console.WriteLine("Result: {0}", result); } Console.WriteLine("Async methods have all completed"); // wait for input before exiting Console.WriteLine("Press enter to finish"); Console.ReadLine(); }