Thread synchronization of events

Brief
AutoResetEvent and ManualResetEvent are derived from the EventWaitHandle class (which is derived from WaitHandle). Mode can be specified when EventWaitHandle tectonic events triggered (EventResetMode enumeration), AutoResetEvent and ManualResetEvent just calls the constructor when constructing EventWaitHandle different parameters, namely,
public  AutoResetEvent( bool  initialState) :  base (initialState, EventResetMode.AutoReset)
{
}

public  ManualResetEvent( bool  initialState) :  base (initialState, EventResetMode.ManualReset)
{
}
Thereby avoiding the dependence on the specific configuration when the class name.

Basic Usage
1. Define events.
2. Set the event waiting (WaitOne, WaitAll, WaitAny) in a thread.
3. In another incident Set event, let the waiting thread continue.

A simple example
using  System;
using  System.Threading;

internal   class  Test
{
    
private   static  EventWaitHandle ewh  =   new  EventWaitHandle( false , EventResetMode.AutoReset);

    
public   static   void  Main()
    {
        Thread t1 
=   new  Thread(Thread1);
        t1.Start();

        Console.WriteLine(
" Start wait 1 " );
        ewh.WaitOne();
        Console.WriteLine(
" Go through 1 " );

        Console.WriteLine(
" Start wait 2 " );
        ewh.WaitOne();
        Console.WriteLine(
" Go through 2 " );
    }

    
public   static   void  Thread1()
    {
        Thread.Sleep(
1000 );

        Console.WriteLine(
" Event Set " );
        ewh.Set();
    }
}
If AutoResetHandle, then after the Set End, ewh will be reset to the state "nonsignaled", if you want to continue to wait for an event, you have to Set again.
If ManualResetHandle, after the Set finished, the state does not automatically reset, but still retains the status "signaled", then you do not have to Set again by waiting for the event. You can be reset by Reset events to "nonsignaled" state.

Auto-reset event to provide exclusive access to resources. If it does not automatically reset when the thread waiting for the event in the end state, the event remains terminated until a thread attempts to wait on the event. The event thread and release immediately reset to prevent later thread.
Manual reset event similar to the entrance. When an event is not in a terminal state, in the event waiting threads will be blocked. When the event is signaled state, all waiting threads are released, and the event has remained termination status (ie, not waiting behind the stop) until its Reset method is called. If a thread must complete an activity, other threads to continue, the manual reset event useful.

Wait a variety of differences
WaitOne () and a variety of heavy-duty, waiting for the current events Set, blocks the current thread (or a specified amount of time).
WaitAll () and a variety of heavy-duty, static methods, the array wait for events all the events have been Set.
WaitAny () and a variety of heavy-duty, static methods, either waiting for the event to get an array of events in a Set.

Reference: http://www.rainsts.net/article.asp?id=170
http://msdn2.microsoft.com/zh-cn/library/system.threading.eventwaithandle.aspx

Reproduced in: https: //www.cnblogs.com/forck/archive/2008/04/16/1155684.html

Guess you like

Origin blog.csdn.net/weixin_33915554/article/details/93549671