![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
using System;
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
using System.Collections.Generic;
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
using System.Linq;
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
using System.Net;
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
using System.Windows;
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
using System.Windows.Controls;
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
using System.Windows.Documents;
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
using System.Windows.Input;
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
using System.Windows.Media;
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
using System.Windows.Media.Animation;
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
using System.Windows.Shapes;
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
namespace Silverlight20.Thread
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
{
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
public partial class EventWaitHandle : UserControl
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
{
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
// AutoResetEvent(bool state) - 通知其他线程是否可入的类,自动 Reset()
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
// bool state - 是否为终止状态,即是否禁止其他线程入内
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
private System.Threading.AutoResetEvent autoResetEvent =
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
new System.Threading.AutoResetEvent( false);
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
// ManualResetEvent(bool state) - 通知其他线程是否可入的类,手动 Reset()
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
// bool state - 是否为终止状态,即是否禁止其他线程入内
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
private System.Threading.ManualResetEvent manualResetEvent =
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
new System.Threading.ManualResetEvent( false);
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
private static int i;
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
public EventWaitHandle()
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
{
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
InitializeComponent();
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
// 演示 AutoResetEvent
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
AutoResetEventDemo();
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
// 演示 ManualResetEvent
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
ManualResetEventDemo();
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
}
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
private void AutoResetEventDemo()
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
{
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
i = 0;
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
for ( int x = 0; x < 100; x++)
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
{
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
// 开 100 个线程去操作静态变量 i
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
System.Threading.Thread thread =
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
new System.Threading.Thread( new System.Threading.ThreadStart(AutoResetEventDemoCallback));
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
thread.Start();
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
// 阻塞当前线程,直到 AutoResetEvent 发出 Set() 信号
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
autoResetEvent.WaitOne();
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
}
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
System.Threading.Thread.Sleep(1000);
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
// 1 秒后 100 个线程都应该执行完毕了,取得 i 的结果
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
txtAutoResetEvent.Text = i.ToString();
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
}
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
private void AutoResetEventDemoCallback()
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
{
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
try
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
{
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
int j = i + 1;
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
// 模拟多线程并发操作静态变量 i 的情况
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
System.Threading.Thread.Sleep(5);
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
i = j;
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
}
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
finally
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
{
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
// 发出 Set() 信号,以释放 AutoResetEvent 所阻塞的线程
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
autoResetEvent.Set();
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
}
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
}
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
private void ManualResetEventDemo()
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
{
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
i = 0;
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
for ( int x = 0; x < 100; x++)
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
{
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
// Reset() - 将 ManualResetEvent 变为非终止状态,即由此线程控制 ManualResetEvent,
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
// 其他线程排队,直到 ManualResetEvent 发出 Set() 信号(AutoResetEvent 在 Set() 时会自动 Reset())
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
manualResetEvent.Reset();
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
// 开 100 个线程去操作静态变量 i
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
System.Threading.Thread thread =
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
new System.Threading.Thread( new System.Threading.ThreadStart(ManualResetEventDemoCallback));
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
thread.Start();
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
// 阻塞当前线程,直到 ManualResetEvent 发出 Set() 信号
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
manualResetEvent.WaitOne();
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
}
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
System.Threading.Thread.Sleep(1000);
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
// 1 秒后 100 个线程都应该执行完毕了,取得 i 的结果
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
txtManualResetEvent.Text = i.ToString();
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
}
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
private void ManualResetEventDemoCallback()
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
{
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
try
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
{
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
int j = i + 1;
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
// 模拟多线程并发操作静态变量 i 的情况
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
System.Threading.Thread.Sleep(5);
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
i = j;
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
}
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
finally
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
{
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
// 发出 Set() 信号,以释放 ManualResetEvent 所阻塞的线程,同时 ManualResetEvent 变为终止状态)
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
manualResetEvent.Set();
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
}
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
}
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
}
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
}