2024年1月28日发(作者:)

_thread = new Thread(e); _ = ("Class:{0}, Id:{1}", "STAThread", _dThreadId); _ground = false; _(); } public void Stop() { // Sync lock (_syncRoot) { // ThreadState if (_threadState != d) throw new InvalidOperationException(); _threadState = ng; // ActionQueue _e(); } // Wait _e(); }

public void Post(SendOrPostCallback callback, object state) { #region Contracts if (callback == null) throw new ArgumentNullException(); #endregion // Action Action action = delegate() { try { callback(state); } catch (Exception ex) { (("Delegate:{0}, State:{1}, Message:{2}", e(), "Exception", e)); } }; // Sync lock (_syncRoot) { // ThreadState if (_threadState != d) throw new InvalidOperationException(); // ActionQueue _e(action); }

} public void Send(SendOrPostCallback callback, object state) { #region Contracts if (callback == null) throw new ArgumentNullException();

#endregion // Action ManualResetEvent actionEvent = new ManualResetEvent(false); Action action = delegate() { try { callback(state); } catch (Exception ex) { (("Delegate:{0}, State:{1}, Message:{2}", e(), "Exception", e)); } finally { (); } }; // Sync lock (_syncRoot) { // ThreadState if (_threadState != d) throw new InvalidOperationException(); // ActionQueue if (tThread != _thread) { _e(action); } } // Execute if (tThread == _thread) { action(); } // Wait e(); }

private void Operate() { try { // Begin _(); // Operate while (true) { // Action Action action = _e(); // Execute if (action != null) { action(); }

// ThreadState if (action == null) { lock (_syncRoot) { if (_threadState == ng) { return; } } } } } finally { // End lock (_syncRoot) { _threadState = d; } _(); } } }}复制代码 代码如下:namespace ing{ public class BlockingQueue { // Fields

private readonly object _syncRoot = new object(); private readonly WaitHandle[] _waitHandles = null; private readonly Queue _itemQueue = null; private readonly Semaphore _itemQueueSemaphore = null; private readonly ManualResetEvent _itemQueueReleaseEvent = null;

// Constructors public BlockingQueue() { // Default _itemQueue = new Queue(); _itemQueueSemaphore = new Semaphore(0, ue); _itemQueueReleaseEvent = new ManualResetEvent(false); _waitHandles = new WaitHandle[] { _itemQueueSemaphore, _itemQueueReleaseEvent }; }

// Methods public void Enqueue(T item) { lock (_syncRoot) { _e(item);

_e(); } } public T Dequeue() { y(_waitHandles); lock (_syncRoot) { if (_ > 0) { return _e(); } } return default(T); } public void Release() { lock (_syncRoot) { _(); } } public void Reset() { lock (_syncRoot) { _(); _(); _(); } } }}