none
The most difference between AutoResetEvent and ManualResetEvent RRS feed

  • Question

  • Can anyone offer me a sample that proves the MOST DIFFERENCE between them?

    Many thanks

    Saturday, August 11, 2012 5:20 AM

Answers

  • The biggest difference is:  The AutoResetEvent resets automatically after unlocking a thread via a wait operation and the other one doesn't.

    A typical use for ManualResetEvent:  You have 100 worker threads and the user decides he/she wants to abort and quit.  You would have a ManualResetEvent to signal all 100 threads to abort and quit.  It must be manual because if it were automatic the first worker thread that acknowledges the order to abort and quit would reset it and the rest of the threads wouldn't get the message.

    A typical use for AutoResetEvent:  You have 100 worker threads waiting for data to work on.  You get this data from, say, a serial or USB port one piece at a time.  So you have all threads waiting on the AutoResetEvent object while the thread picking up data collects one complete piece of information.  Once the information is collected it is saved into a shared variable (shared among all threads) and then the event is signaled.  Since there are many threads waiting for the event to signal, you MUST use an auto reset object because if you don't, more than one thread would start working on the same piece of data, which would be a waste at the very least.


    Jose R. MCP
    Code Samples

    • Marked as answer by TimoYang Saturday, August 11, 2012 6:01 AM
    Saturday, August 11, 2012 5:43 AM
  • An example please……It's easy for me to understand……XD

    namespace CSharp
    {
        public class WaitHandlerExample
        {
            public static AutoResetEvent waitHandler;
            public static ManualResetEvent manualWaitHandler;
     
            public static void ThreadPoolMain()
            {
                waitHandler = new AutoResetEvent(false);
                manualWaitHandler = new ManualResetEvent(false);
     
                // Queue the task.
                ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc));
                ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc2));
     
                Console.WriteLine("Main thread does some work, then waiting....");
                //waitHandler.WaitOne();
                //waitHandler.WaitOne();
                manualWaitHandler.WaitOne();
                manualWaitHandler.Reset();
                manualWaitHandler.WaitOne();
                //waitHandler.Reset();
                Console.WriteLine("Main thread exits.");
            }
     
            // This thread procedure performs the task.
            public static void ThreadProc(Object stateInfo)
            {
                Thread.Sleep(1000);
                Console.WriteLine("Hello from the thread pool.");
                //waitHandler.Set();        //
               manualWaitHandler.Set();
                //manualWaitHandler.Reset();
            }
            public static void ThreadProc2(object stateInfo)
            {
                Thread.Sleep(100);
                Console.WriteLine("Hello from the thread Pool2");
                //waitHandler.Set();
                manualWaitHandler.Set();
            }         static void Main(string[] args)         {             WaitHandlerExample.ThreadPoolMain();         }     } }
    • Edited by TimoYang Saturday, August 11, 2012 6:01 AM
    • Marked as answer by TimoYang Saturday, August 11, 2012 6:01 AM
    Saturday, August 11, 2012 5:47 AM

All replies

  • The biggest difference is:  The AutoResetEvent resets automatically after unlocking a thread via a wait operation and the other one doesn't.

    A typical use for ManualResetEvent:  You have 100 worker threads and the user decides he/she wants to abort and quit.  You would have a ManualResetEvent to signal all 100 threads to abort and quit.  It must be manual because if it were automatic the first worker thread that acknowledges the order to abort and quit would reset it and the rest of the threads wouldn't get the message.

    A typical use for AutoResetEvent:  You have 100 worker threads waiting for data to work on.  You get this data from, say, a serial or USB port one piece at a time.  So you have all threads waiting on the AutoResetEvent object while the thread picking up data collects one complete piece of information.  Once the information is collected it is saved into a shared variable (shared among all threads) and then the event is signaled.  Since there are many threads waiting for the event to signal, you MUST use an auto reset object because if you don't, more than one thread would start working on the same piece of data, which would be a waste at the very least.


    Jose R. MCP
    Code Samples

    • Marked as answer by TimoYang Saturday, August 11, 2012 6:01 AM
    Saturday, August 11, 2012 5:43 AM
  • An example please……It's easy for me to understand……XD

    namespace CSharp
    {
        public class WaitHandlerExample
        {
            public static AutoResetEvent waitHandler;
            public static ManualResetEvent manualWaitHandler;
     
            public static void ThreadPoolMain()
            {
                waitHandler = new AutoResetEvent(false);
                manualWaitHandler = new ManualResetEvent(false);
     
                // Queue the task.
                ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc));
                ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc2));
     
                Console.WriteLine("Main thread does some work, then waiting....");
                //waitHandler.WaitOne();
                //waitHandler.WaitOne();
                manualWaitHandler.WaitOne();
                manualWaitHandler.Reset();
                manualWaitHandler.WaitOne();
                //waitHandler.Reset();
                Console.WriteLine("Main thread exits.");
            }
     
            // This thread procedure performs the task.
            public static void ThreadProc(Object stateInfo)
            {
                Thread.Sleep(1000);
                Console.WriteLine("Hello from the thread pool.");
                //waitHandler.Set();        //
               manualWaitHandler.Set();
                //manualWaitHandler.Reset();
            }
            public static void ThreadProc2(object stateInfo)
            {
                Thread.Sleep(100);
                Console.WriteLine("Hello from the thread Pool2");
                //waitHandler.Set();
                manualWaitHandler.Set();
            }         static void Main(string[] args)         {             WaitHandlerExample.ThreadPoolMain();         }     } }
    • Edited by TimoYang Saturday, August 11, 2012 6:01 AM
    • Marked as answer by TimoYang Saturday, August 11, 2012 6:01 AM
    Saturday, August 11, 2012 5:47 AM