Mega Code Archive

 
Categories / C# / Thread
 

ThreadPool RegisterWaitForSingleObject

using System; using System.Threading; class MainClass {     private static void EventHandler(object state, bool timedout)     {         if (timedout)         {             Console.WriteLine("{0} : Wait timed out.", DateTime.Now.ToString("HH:mm:ss.ffff"));         }         else         {             Console.WriteLine("{0} : {1}", DateTime.Now.ToString("HH:mm:ss.ffff"), state);         }     }     public static void Main()     {         AutoResetEvent autoEvent = new AutoResetEvent(false);         string state = "AutoResetEvent signaled.";         RegisteredWaitHandle handle = ThreadPool.RegisterWaitForSingleObject(autoEvent, EventHandler, state, 3000, false);         while (Console.ReadLine().ToUpper() != "CANCEL")         {             autoEvent.Set();         }         Console.WriteLine("Unregistering wait operation.");         handle.Unregister(null);     } }