c# – 如何解除在AutoResetEvent对象上调用WaitOne方法的线程?

前端之家收集整理的这篇文章主要介绍了c# – 如何解除在AutoResetEvent对象上调用WaitOne方法的线程?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
下面是一个类,其方法“SomeMethod”说明了我的问题.
  1. class SomeClass
  2. {
  3. AutoResetEvent theEvent = new AutoResetEvent(false);
  4. // more member declarations
  5.  
  6. public void SomeMethod()
  7. {
  8. // some code
  9. theEvent.WaitOne();
  10. // more code
  11. }
  12. }

方法设计为线程安全,并将在不同的线程中调用.现在我的问题是可以在任何时候解除阻止在’theEvent’对象上调用’WaitOne’方法的所有线程?这个要求经常出现在我的设计中,因为我需要能够正常地停止并启动我的多线程程序.在我看来,启动一个多线程程序是相当简单的,但是很难阻止它.

这是我迄今为止所尝试的,这显然是有效的.但这是标准方法吗?

  1. public void UnblockAll()
  2. {
  3. do
  4. {
  5. theEvent.Set();
  6. } while (theEvent.WaitOne(0));
  7. }

‘UnblockAll’方法是’SomeClass’类的成员.这里使用的技术是基于WaitOne method的MSDN文档.我引用以下文档的相关部分:

If millisecondsTimeout is zero,the method does not block. It tests the state of the wait handle and returns immediately.

在do..while循环中,我调用Set method.这释放了一个可能由于调用WaitOne方法(在“SomeMethod”方法中编码)可能阻塞的单个线程.接下来我测试’theEvent’对象的状态,只是为了知道它是否发信号.这个测试是通过调用超时参数的WaitOne方法的重载版来完成的.当我调用WaitOne方法时,我使用的参数为零,根据文档,这个参数会导致立即返回一个布尔值的调用.如果返回值为true,那么’theEvent’对象处于信号状态.如果在’SomeMethod’方法中对’WaitOne’方法调用中至少有一个线程被阻塞,则对’Set’方法调用(在’UnblockAll’方法中编码)将会解除阻塞.因此,在“UnblockAll”方法中对do..while语句结尾处的“WaitOne”方法调用将返回false.只有当没有线程被阻塞时,返回值才是真的.

上述推理是否正确,如果是正确的,技术是处理我的问题的标准方法?我正在尝试使用该解决方案主要在.net compact-framework 2.0平台上.

解决方法

你有三个可行的选择.每个人都有自己的优点和缺点.选择最适合您特定情况的.

选项1 – 轮询WaitHandle.

如果没有给出关闭请求,而不是执行无限制的阻塞调用,使用具有超时的缓冲区来恢复该块.

  1. public void SomeMethod()
  2. {
  3. while (!yourEvent.WaitOne(POLLING_INTERVAL))
  4. {
  5. if (IsShutdownRequested())
  6. {
  7. // Add code to end gracefully here.
  8. }
  9. }
  10. // Your event was signaled so now we can proceed.
  11. }

选项2 – 使用单独的WaitHandle请求关闭

  1. public void SomeMethod()
  2. {
  3. WaitHandle[] handles = new WaitHandle[] { yourEvent,shutdownEvent };
  4. if (WaitHandle.WaitAny(handles) == 1)
  5. {
  6. // Add code to end gracefully here.
  7. }
  8. // Your event was signaled so now we can proceed.
  9. }

选项3 – 使用Thread.Interrupt

不要将此与Thread.Abort混淆.中断线程绝对不安全,但是中断线程是完全不同的. Thread.Interrupt将“戳”BCL中使用的内置阻塞调用,包括Thread.Join,WaitHandle.WaitOne,Thread.Sleep等.

猜你在找的C#相关文章