Пример #1
0
        private TaskCommon TaskListRemoveFirst()
        {
            if (_taskCommonListHead == null)
            {
                return(null);
            }
            if (object.ReferenceEquals(_taskCommonListHead._next, _taskCommonListHead))
            {
                TaskCommon taskCommonListHead = _taskCommonListHead;
                _taskCommonListHead = null;
                _taskCommonCount--;
                taskCommonListHead._next     = null;
                taskCommonListHead._previous = null;
                return(taskCommonListHead);
            }
            TaskCommon taskCommonListHead2 = _taskCommonListHead;

            _taskCommonListHead                 = _taskCommonListHead._next;
            _taskCommonListHead._previous       = taskCommonListHead2._previous;
            _taskCommonListHead._previous._next = _taskCommonListHead;
            _taskCommonCount--;
            taskCommonListHead2._next     = null;
            taskCommonListHead2._previous = null;
            return(taskCommonListHead2);
        }
Пример #2
0
 private void TaskListAddLast(TaskCommon Item)
 {
     if (_taskCommonListHead == null)
     {
         _taskCommonListHead = Item;
         Item._next          = Item;
         Item._previous      = Item;
     }
     else
     {
         _taskCommonListHead._previous._next = Item;
         Item._previous = _taskCommonListHead._previous;
         Item._next     = _taskCommonListHead;
         _taskCommonListHead._previous = Item;
     }
     _taskCommonCount++;
 }
Пример #3
0
        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);
        }