/// <summary> /// The Execute method is the entrypoint for the system /// thread pool to execute tasks on the task queue. /// </summary> /// <param name="ignored"> /// The ignored parameter is provided in order for the /// method to match the WaitCallback signature which is /// required by the .NET QueueUserWorkItem method. /// </param> private void Execute(object ignored) { while (true) { SequencedTask task = null; lock (_taskQueue) { if (_taskQueue.Count == 0) { _pending = false; break; } task = _taskQueue.Dequeue(); } try { task.Execute(); } catch (System.Exception) { ThreadPool.UnsafeQueueUserWorkItem(Execute, null); throw; } } }
/// <summary> /// The Enqueue method is invoked to add a callback action /// to the sequencer's queue of operations. /// </summary> /// <param name="callback"> /// The callback that is to be invoked. /// </param> /// <param name="state"> /// The state information to be passed to the callback /// method when it is executed. /// </param> public void Enqueue(ContextCallback callback, object state) { SequencedTask task = new SequencedTask(callback, state, ExecutionContext.Capture()); lock (_taskQueue) { _taskQueue.Enqueue(task); if (_pending == false) { _pending = true; ThreadPool.UnsafeQueueUserWorkItem(Execute, null); } } }