0

Will close() signal all the threads that have called WaitOne()?

If not, what's the best way to do this?

poy
  • 10,063
  • 9
  • 49
  • 74

2 Answers2

1

No. It will cause the WaitOne() method to fail with an exception, ObjectDisposedException in particular.

The only "best way" is to not do this, it is a plain bug. EventWaitHandle objects should only ever be closed or disposed when they are no longer used.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
0

It will cause an AbandonedMutexException. The correct way of using this class is not to call close while waiters are active.

See http://msdn.microsoft.com/en-us/library/58195swd.aspx

usr
  • 168,620
  • 35
  • 240
  • 369
  • So if I have threads that are waiting, and I want to terminate for some reason, whats the best way to wake up the threads? If I use set, it just wakes up ONE... – poy Jan 17 '13 at 20:16
  • That exception is highly specific to the Mutex class. Raised when the thread that owned the mutex terminated without releasing the mutex. – Hans Passant Jan 17 '13 at 20:19
  • @HansPassant I actually looked it up - it also applies to the EventWaitHandle class. – usr Jan 17 '13 at 21:15
  • I'm quite sure about this. Feel free to document your answer appropriately. – Hans Passant Jan 17 '13 at 21:16
  • The docs state the exceptions: http://msdn.microsoft.com/en-us/library/58195swd.aspx (have not tested this). – usr Jan 17 '13 at 21:53