public virtual bool Enqueue(ITask task) { ITask task2 = null; bool flag = false; if (task == null) { throw new ArgumentNullException("task"); } task.TaskQueue = this; if (_dispatcher == null) { _scheduledTaskCount += 1L; ThreadPool.QueueUserWorkItem(new WaitCallback(TaskExecutionWorker.ExecuteInCurrentThreadContext), task); return(task2 == null); } lock (_taskQueue) { if (_isDisposed) { if ((_dispatcher.Options & DispatcherOptions.SuppressDisposeExceptions) == DispatcherOptions.None) { throw new ObjectDisposedException(typeof(DispatcherQueue).Name + ":" + Name); } return(false); } else { switch (_policy) { case TaskExecutionPolicy.Unconstrained: { TaskCommon taskCommon = task as TaskCommon; if (taskCommon != null) { TaskListAddLast(taskCommon); } else { _taskQueue.Enqueue(task); } break; } case TaskExecutionPolicy.ConstrainQueueDepthDiscardTasks: RecalculateSchedulingRate(); if (_taskQueue.Count >= _maximumQueueDepth) { Dispatcher.LogInfo("DispatcherQueue.Enqueue: Discarding oldest task because queue depth limit reached"); ITask task3; TryDequeue(out task3); task2 = task3; } _taskQueue.Enqueue(task); break; case TaskExecutionPolicy.ConstrainQueueDepthThrottleExecution: RecalculateSchedulingRate(); if (_taskQueue.Count >= _maximumQueueDepth) { Dispatcher.LogInfo("DispatcherQueue.Enqueue: Forcing thread sleep because queue depth limit reached"); while (_taskQueue.Count >= _maximumQueueDepth) { Sleep(); } flag = true; } _taskQueue.Enqueue(task); break; case TaskExecutionPolicy.ConstrainSchedulingRateDiscardTasks: RecalculateSchedulingRate(); if (_currentSchedulingRate >= _maximumSchedulingRate) { Dispatcher.LogInfo("DispatcherQueue.Enqueue: Discarding task because task scheduling rate exceeded"); ITask task4; TryDequeue(out task4); task2 = task4; } _scheduledItems += 1.0; _taskQueue.Enqueue(task); break; case TaskExecutionPolicy.ConstrainSchedulingRateThrottleExecution: RecalculateSchedulingRate(); if (_currentSchedulingRate >= _maximumSchedulingRate) { Dispatcher.LogInfo("DispatcherQueue.Enqueue: Forcing thread sleep because task scheduling rate exceeded"); while (_currentSchedulingRate > _maximumSchedulingRate) { Sleep(); RecalculateSchedulingRate(); } flag = true; } _scheduledItems += 1.0; _taskQueue.Enqueue(task); break; } _scheduledTaskCount += 1L; SignalDispatcher(); } } if (task2 != null || flag) { TaskExecutionPolicyEngaged(task2, flag); } return(task2 == null); }