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 further Enqueue() operations to throw EndOfStreamException, and all pending or subsequent Dequeue() operations to throw an EndOfStreamException once the queue is empty.

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 Dequeue()

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

public object DequeueNoWait(object defaultValue)

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

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 further Enqueue() operations to throw EndOfStreamException, and all pending or subsequent Dequeue() operations to throw an EndOfStreamException once the queue is empty.

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 no items are present and the queue is in a closed state, or if at any time while waiting the queue transitions to a closed state (by a call to Close()), this method will 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. In the latter case 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 empty and in a closed state (following a call to Close()), then this method will throw EndOfStreamException.

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.