Execute a Method by Signaling a WaitHandle Object - CSharp Reflection

CSharp examples for Reflection:Method

Description

Execute a Method by Signaling a WaitHandle Object

Demo Code


using System;//from  w  w  w.j av  a  2 s.  co  m
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, 10000, false);

            Console.WriteLine("Press ENTER to signal the AutoResetEvent" +" or enter \"Cancel\" to unregister the wait operation.");

            while (Console.ReadLine().ToUpper() != "CANCEL")
            {
                autoEvent.Set();
            }
            Console.WriteLine("Unregistering wait operation.");
            handle.Unregister(null);

        }
    }

Result


Related Tutorials