暂停线程执行

 1 private readonly ManualResetEvent _resumeEvent = new ManualResetEvent(false);        
 2 private volatile bool _blnPaused; //记录运行状态是否暂停
 3 
 4         /// <summary>
 5         /// 暂停
 6         /// </summary>
 7         public void Pause()
 8         {
 9             _resumeEvent.Reset();
10             _blnPaused = true;
11         }
12 
13         /// <summary>
14         /// 重启
15         /// </summary>
16         public void Resume()
17         {
18             _blnPaused = false;
19             _resumeEvent.Set();
20         }
21 
22 ...
23 
24 线程方法中添加下面的代码
25 
26           if (_blnPaused) //如果暂停则等待
27           {
28              _resumeEvent.WaitOne();
29           }
30           else
31           {
32

 

posted @ 2014-04-21 10:20  DR19  阅读(210)  评论(0编辑  收藏  举报