You can use classes derived from the WaitHandle class to trigger the execution of a method. Using the RegisterWaitForSingleObject method of the ThreadPool class, you can register a WaitOrTimerCallback delegate instance for execution by a thread-pool thread when a specified WaitHandle-derived object enters a signaled state. You can configure the thread pool to execute the method only once or to automatically reregister the method for execution each time the WaitHandle is signaled. If the WaitHandle is already signaled when you call RegisterWaitForSingleObject, the method will execute immediately. The Unregister method of the System.Threading.RegisteredWaitHandle object returned by the RegisterWaitForSingleObject method is used to cancel a registered wait operation.
The class most commonly used as a trigger is AutoResetEvent, which automatically returns to an unsignaled state after it's signaled. However, you can also use the ManualResetEvent and Mutex classes, which require you to change the signaled state manually. The following example demonstrates the use of an AutoResetEvent to trigger the execution of a method named EventHandler.
using System; using System.Threading; public class EventExecutionExample { // A method that is executed when the AutoResetEvent is signaled // or the wait operation times out. private static void EventHandler(object state, bool timedout) { // Display appropriate message to the console based on whether // the wait timed out or the AutoResetEvent was signaled. 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() { // Create the new AutoResetEvent in an unsignaled state. AutoResetEvent autoEvent = new AutoResetEvent(false); // Create a new WaitOrTimerCallback delegate instance that // references the static EventHandler method. EventHandler // will be called when the AutoResetEvent is signaled or // the wait times out. WaitOrTimerCallback handler = new WaitOrTimerCallback(EventHandler); // Create the state object that is passed to the event handler // method when it is triggered. In this case a message to display. string state = "AutoResetEvent signaled."; // Register the delegate instance to wait for the AutoResetEvent to // be signaled. Set a time-out of 3 seconds, and configure the wait // operation to reset after activation (last argument). RegisteredWaitHandle handle = ThreadPool.RegisterWaitForSingleObject(autoEvent, handler, state, 3000, false); Console.WriteLine("Press ENTER to signal the AutoResetEvent" + " or enter \"Cancel\" to unregister the wait operation."); while (Console.ReadLine().ToUpper() != "CANCEL") { // If "Cancel" has not been entered into the console, signal // the AutoResetEvent, which will cause the EventHandler // method to execute. The AutoResetEvent will automatically // revert to an unsignaled state. autoEvent.Set(); } // Unregister the wait operation. Console.WriteLine("Unregistering wait operation."); handle.Unregister(null); // Wait to continue. Console.WriteLine("Main method complete. Press Enter."); Console.ReadLine(); } }
No comments:
Post a Comment