ThreadPool.RegisterWaitForSingleObject : Thread Pool « Thread « C# / C Sharp






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

 








Related examples in the same category

1.ThreadPool.QueueUserWorkItem
2.Thread pool demoThread pool demo
3.illustrates the use of the system thread poolillustrates the use of the system thread pool
4.Thread Pool Tcp Server
5.Thread Pool SampleThread Pool Sample