using System; using System.Collections.Generic; using System.Text; using System.Threading; namespace SN_Server { public class InterthreadMessageQueue { System.Collections.Generic.Queue _queue = new System.Collections.Generic.Queue(); /// /// Post a message to the queue. /// public void PostItem(T item) { lock (_queue) { _queue.Enqueue(item); if (_queue.Count == 1) Monitor.Pulse(_queue); } } /// /// Retrieve a message from the queue. /// /// Number of milliseconds to block ifnothing is available. -1 means "block indefinitely" /// The next item in the queue, or default(T) if queue is empty public T GetItem(int maxWait) { lock (_queue) { if (_queue.Count == 0) { if (maxWait == 0) return default(T); Monitor.Wait(_queue, maxWait); if (_queue.Count == 0) return default(T); } return _queue.Dequeue(); } } public int Count { get { lock (_queue) { return _queue.Count; } } } } }