Mega Code Archive

 
Categories / C# Tutorial / Thread
 

Asynchronous Results Pattern Example

using System; using System.Threading; delegate void WorkerThreadHandler(); public class MainClass {   public static AutoResetEvent ResetEvent = new AutoResetEvent(false);   public static void Main()   {       WorkerThreadHandler workerMethod = null;       IAsyncResult asyncResult = null;       try       {           workerMethod = new WorkerThreadHandler(DoWork);           asyncResult = workerMethod.BeginInvoke(null, null);           while(!asyncResult.AsyncWaitHandle.WaitOne(1000, false))           {               Console.Write('.');           }       }       finally       {           if (workerMethod != null && asyncResult != null)           {              workerMethod.EndInvoke(asyncResult);           }       }   }   public static void DoWork()   {       Thread.Sleep(1000);   } }