ManualResetEvent和AutoResetEvent

  
    
// ManualResetEvent reset=new ManualResetEvent(false);
AutoResetEvent reset = new AutoResetEvent( false );
ThreadPool.QueueUserWorkItem((WaitCallback)
delegate
{
Console.WriteLine(
" Thread 1 wait one.. " );
reset.WaitOne();
Console.WriteLine(
" Thread 1 got " );
});

ThreadPool.QueueUserWorkItem((WaitCallback)
delegate
{
Console.WriteLine(
" Thread 2 wait one.. " );
reset.WaitOne();
Console.WriteLine(
" Thread 2 got " );
});
ThreadPool.QueueUserWorkItem((WaitCallback)
delegate
{
Console.WriteLine(
" Thread 3 wait one.. " );
reset.WaitOne();
Console.WriteLine(
" Thread 3 got " );
});
Console.ReadLine();
reset.Set();
Console.WriteLine(
" ok " );

二者区别在于:AutoResetEvent 会在获取到控制权时自动设置事件为非终止状态。常用于多线程中独占资源,类似lock

你可能感兴趣的:(event)