Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you're using a <code>ManualResetEvent</code> with anonymous methods then it's obviously useful. But as Sam mentioned they can often be passed around into workers, and then set and closed.</p> <p>So I would say it depends on the context of how you are using it - <a href="http://msdn.microsoft.com/en-us/library/z6w25xa6.aspx" rel="nofollow noreferrer">the MSDN WaitHandle.WaitAll()</a> code sample has a good example of what I mean. </p> <p>Here's an example based on the MSDN sample of how creating the WaitHandles with a <code>using</code> statement would exception:</p> <blockquote> <p><strong>System.ObjectDisposedException</strong><br> "Safe handle has been closed"</p> </blockquote> <pre><code>const int threads = 25; void ManualWaitHandle() { ManualResetEvent[] manualEvents = new ManualResetEvent[threads]; for (int i = 0; i &lt; threads; i++) { using (ManualResetEvent manualResetEvent = new ManualResetEvent(false)) { ThreadPool.QueueUserWorkItem(new WaitCallback(ManualWaitHandleThread), new FileState("filename", manualResetEvent)); manualEvents[i] = manualResetEvent; } } WaitHandle.WaitAll(manualEvents); } void ManualWaitHandleThread(object state) { FileState filestate = (FileState) state; Thread.Sleep(100); filestate.ManualEvent.Set(); } class FileState { public string Filename { get;set; } public ManualResetEvent ManualEvent { get; set; } public FileState(string fileName, ManualResetEvent manualEvent) { Filename = fileName; ManualEvent = manualEvent; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload