EventQueue
Implements a queue of work items each of which
is queued as a WaitCallback.
using System.Collections;
using System.Threading;
namespace NUnit.Framework.Internal.Execution
{
public class EventQueue
{
private readonly Queue queue = new Queue();
private readonly object syncRoot;
private bool stopped;
private AutoResetEvent synchronousEventSent;
public int Count {
get {
lock (syncRoot) {
return queue.Count;
}
}
}
public EventQueue()
{
syncRoot = queue.SyncRoot;
}
public void SetWaitHandleForSynchronizedEvents(AutoResetEvent synchronousEventWaitHandle)
{
synchronousEventSent = synchronousEventWaitHandle;
}
public void Enqueue(Event e)
{
lock (syncRoot) {
queue.Enqueue(e);
Monitor.Pulse(syncRoot);
}
if (synchronousEventSent != null && e.IsSynchronous)
synchronousEventSent.WaitOne();
else
Thread.Sleep(0);
}
public Event Dequeue(bool blockWhenEmpty)
{
lock (syncRoot) {
while (queue.Count == 0) {
if (!blockWhenEmpty || stopped)
return null;
Monitor.Wait(syncRoot);
}
return (Event)queue.Dequeue();
}
}
public void Stop()
{
lock (syncRoot) {
if (!stopped) {
stopped = true;
Monitor.PulseAll(syncRoot);
}
}
}
}
}