- class SomeClass
- {
- AutoResetEvent theEvent = new AutoResetEvent(false);
- // more member declarations
- public void SomeMethod()
- {
- // some code
- theEvent.WaitOne();
- // more code
- }
- }
该方法设计为线程安全,并将在不同的线程中调用.现在我的问题是可以在任何时候解除阻止在’theEvent’对象上调用’WaitOne’方法的所有线程?这个要求经常出现在我的设计中,因为我需要能够正常地停止并启动我的多线程程序.在我看来,启动一个多线程程序是相当简单的,但是很难阻止它.
这是我迄今为止所尝试的,这显然是有效的.但这是标准方法吗?
- public void UnblockAll()
- {
- do
- {
- theEvent.Set();
- } while (theEvent.WaitOne(0));
- }
‘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.
如果没有给出关闭请求,而不是执行无限制的阻塞调用,使用具有超时的缓冲区来恢复该块.
- public void SomeMethod()
- {
- while (!yourEvent.WaitOne(POLLING_INTERVAL))
- {
- if (IsShutdownRequested())
- {
- // Add code to end gracefully here.
- }
- }
- // Your event was signaled so now we can proceed.
- }
选项2 – 使用单独的WaitHandle请求关闭
- public void SomeMethod()
- {
- WaitHandle[] handles = new WaitHandle[] { yourEvent,shutdownEvent };
- if (WaitHandle.WaitAny(handles) == 1)
- {
- // Add code to end gracefully here.
- }
- // Your event was signaled so now we can proceed.
- }
选项3 – 使用Thread.Interrupt
不要将此与Thread.Abort混淆.中断线程绝对不安全,但是中断线程是完全不同的. Thread.Interrupt将“戳”BCL中使用的内置阻塞调用,包括Thread.Join,WaitHandle.WaitOne,Thread.Sleep等.