Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The important thing to remember is that you must set the <code>SafeWaitHandle</code> property.</p> <p>From MSDN:</p> <blockquote> <p>When you derive from WaitHandle, use the SafeWaitHandle property to store your native handle operating system handle. You do not need to override the protected Dispose method unless you use additional unmanaged resources.</p> </blockquote> <p>Here is how I would do this.</p> <pre><code>public class QueueWaitHandle&lt;T&gt; : WaitHandle { private Queue&lt;T&gt; queue = new Queue&lt;T&gt;(); private ManualResetEvent signal = new ManualResetEvent(false); public QueueWaitHandle() { base.SafeWaitHandle = signal.SafeWaitHandle; } public void Enqueue(T item) { lock (queue) { queue.Enqueue(item); signal.Set(); } } public T Dequeue() { lock (queue) { T item = queue.Dequeue(); if (queue.Count == 0) { signal.Reset(); } return item; } } } </code></pre> <p>When done this way <code>Enqueue</code> acts like the <code>Set</code> method and <code>Dequeue</code> acts like the <code>Reset</code> method. So basically this works like a counting event where non-zero is signaled and zero is unsignaled. The queue is doing the counting in this case and it just happens to also hold data.</p> <p>I know you were asking about subclassing <code>WaitHandle</code> in general, but this specific data structure is more than just an exercise. It can be useful in some scenarios. I would not, however, call it a waitable queue because that implies, at least to me anyway, that the <code>Dequeue</code> operation will block when the queue is empty. Clearly that is not what will happen in this particular implemenation.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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