public class SharedQueue

Summary

A thread-safe shared queue implementation.

Constructor Summary

Flags Name Summary
public SharedQueue()

Construct a fresh, empty SharedQueue.

Method Summary

Flags Name Summary
public void Close()

Close the queue. Causes all waiting threads to be interrupted (with EndOfStreamException) and all further Enqueue() and Dequeue() operations to throw EndOfStreamException.

public object Dequeue()

Retrieve the first item from the queue, or block if none available

public bool Dequeue(int millisecondsTimeout, out object result)

Retrieve the first item from the queue, or return nothing if no items are available after the given timeout

public object DequeueNoWait(object defaultValue)

Retrieve the first item from the queue, or return defaultValue immediately if no items are available

public virtual final void Dispose()

Implement IDisposable.Dispose. Delegates directly to Close().

public void Enqueue(object o)

Place an item at the end of the queue.

Constructor Detail

SharedQueue

public SharedQueue()

Summary

Construct a fresh, empty SharedQueue.

Method Detail

Close

public void Close()

Flags public
Return type void

Summary

Close the queue. Causes all waiting threads to be interrupted (with EndOfStreamException) and all further Enqueue() and Dequeue() operations to throw EndOfStreamException.

Dequeue

public object Dequeue()

Flags public
Return type object

Summary

Retrieve the first item from the queue, or block if none available

Remarks

Callers of Dequeue() will block if no items are available until some other thread calls Enqueue() or the queue is closed. If the queue is closed (by a call to Close()), this method will throw EndOfStreamException.

Dequeue

public bool Dequeue(int millisecondsTimeout, out object result)

Flags public
Return type bool
Parameters
Name Type
millisecondsTimeout int
result out object

Summary

Retrieve the first item from the queue, or return nothing if no items are available after the given timeout

Remarks

If one or more items are present on the queue at the time the call is made, the call will return immediately. Otherwise, the calling thread blocks until either an item appears on the queue, or millisecondsTimeout milliseconds have elapsed.

Returns true in the case that an item was available before the timeout, in which case the out parameter "result" is set to the item itself.

If no items were available before the timeout, returns false, and sets "result" to null.

A timeout of -1 (i.e. System.Threading.Timeout.Infinite) will be interpreted as a command to wait for an indefinitely long period of time for an item to become available. Usage of such a timeout is equivalent to calling Dequeue() with no arguments. See also the MSDN documentation for System.Threading.Monitor.Wait(object,int).

If, at any time during the call, the queue is in or transitions to a closed state (by a call to Close()), this method will throw EndOfStreamException.

DequeueNoWait

public object DequeueNoWait(object defaultValue)

Flags public
Return type object
Parameters
Name Type
defaultValue object

Summary

Retrieve the first item from the queue, or return defaultValue immediately if no items are available

Remarks

If one or more objects are present in the queue at the time of the call, the first item is removed from the queue and returned. Otherwise, the defaultValue that was passed in is returned immediately. This defaultValue may be null, or in cases where null is part of the range of the queue, may be some other sentinel object. The difference between DequeueNoWait() and Dequeue() is that DequeueNoWait() will not block when no items are available in the queue, whereas Dequeue() will.

If, at the time of call, the queue is in a closed state (by a call to Close()), this method will throw EndOfStreamException.

Dispose

public virtual final void Dispose()

Flags public virtual final
Return type void

Summary

Implement IDisposable.Dispose. Delegates directly to Close().

Enqueue

public void Enqueue(object o)

Flags public
Return type void
Parameters
Name Type
o object

Summary

Place an item at the end of the queue.

Remarks

If there is a thread waiting for an item to arrive, the waiting thread will be woken, and the newly Enqueued item will be passed to it. If the queue is closed on entry to this method, EndOfStreamException will be thrown.