ThreadPool.RegisterWaitForSingleObject

image_pdfimage_print
   
 
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);
    }
}

    


This entry was posted in Thread. Bookmark the permalink.