Is it possible (in a meaningful way) ?
For example, say I’d like to implement a waitable queue as follows:
public class WaitableQueue : WaitHandle {
public WaitableQueue() {
q = new Queue();
}
public void Enqueue(Object Arg) {
lock(this) {
q.Enqueue(Arg);
// set the waithandle here (how?)
}
}
public Type Dequeue() {
lock(this) {
if(q.Count == 1)
// reset the waithandle here (how?)
return q.Dequeue();
}
}
Queue q;
}
The important thing to remember is that you must set the
SafeWaitHandleproperty.From MSDN:
Here is how I would do this.
When done this way
Enqueueacts like theSetmethod andDequeueacts like theResetmethod. 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.I know you were asking about subclassing
WaitHandlein 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 theDequeueoperation will block when the queue is empty. Clearly that is not what will happen in this particular implemenation.