using System; using System.Threading; class Program { public delegate int MathOperation(int a, int b); public static int Add(int a, int b) { Thread.Sleep(2000); return a + b; } static void Main(string[] args) { Console.WriteLine("Main Started"); MathOperation add = Add; Console.WriteLine("\nSynchronous call:"); int syncResult = add(5, 3); Console.WriteLine($"Synchronous result: {syncResult}"); Console.WriteLine("\nAsync call using BeginInvoke:"); IAsyncResult asyncResult = add.BeginInvoke(20, 20, null, null); Console.WriteLine("Main thread continues to run..."); int asyncResultValue = add.EndInvoke(asyncResult); Console.WriteLine($"Asynchronous result: {asyncResultValue}"); Console.WriteLine("Main Ended"); } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter