예제 #1
0
        protected override void QueueTask(Task task)
        {
            if (this._disposeCancellation.IsCancellationRequested)
            {
                throw new ObjectDisposedException(base.GetType().Name);
            }
            if (this._targetScheduler == null)
            {
                this._blockingTaskQueue.Add(task);
                return;
            }
            bool flag = false;

            lock (this._nonthreadsafeTaskQueue)
            {
                this._nonthreadsafeTaskQueue.Enqueue(task);
                if (this._delegatesQueuedOrRunning < this._concurrencyLevel)
                {
                    QueuedTaskScheduler queuedTaskScheduler = this;
                    queuedTaskScheduler._delegatesQueuedOrRunning = queuedTaskScheduler._delegatesQueuedOrRunning + 1;
                    flag = true;
                }
            }
            if (flag)
            {
                Task.Factory.StartNew(new Action(this.ProcessPrioritizedAndBatchedTasks), CancellationToken.None, TaskCreationOptions.None, this._targetScheduler);
            }
        }
예제 #2
0
        private void ProcessPrioritizedAndBatchedTasks()
        {
            Task task;
            bool flag = true;

            while (!this._disposeCancellation.IsCancellationRequested && flag)
            {
                try
                {
                    QueuedTaskScheduler._taskProcessingThread.Value = true;
                    while (!this._disposeCancellation.IsCancellationRequested)
                    {
                        lock (this._nonthreadsafeTaskQueue)
                        {
                            if (this._nonthreadsafeTaskQueue.Count != 0)
                            {
                                task = this._nonthreadsafeTaskQueue.Dequeue();
                            }
                            else
                            {
                                break;
                            }
                        }
                        QueuedTaskScheduler.QueuedTaskSchedulerQueue queuedTaskSchedulerQueue = null;
                        if (task == null)
                        {
                            lock (this._queueGroups)
                            {
                                this.FindNextTask_NeedsLock(out task, out queuedTaskSchedulerQueue);
                            }
                        }
                        if (task == null)
                        {
                            continue;
                        }
                        if (queuedTaskSchedulerQueue == null)
                        {
                            base.TryExecuteTask(task);
                        }
                        else
                        {
                            queuedTaskSchedulerQueue.ExecuteTask(task);
                        }
                    }
                }
                finally
                {
                    lock (this._nonthreadsafeTaskQueue)
                    {
                        if (this._nonthreadsafeTaskQueue.Count == 0)
                        {
                            QueuedTaskScheduler queuedTaskScheduler = this;
                            queuedTaskScheduler._delegatesQueuedOrRunning = queuedTaskScheduler._delegatesQueuedOrRunning - 1;
                            flag = false;
                            QueuedTaskScheduler._taskProcessingThread.Value = false;
                        }
                    }
                }
            }
        }
예제 #3
0
 /// <summary>Initializes the debug view.</summary>
 /// <param name="scheduler">The scheduler.</param>
 public QueuedTaskSchedulerDebugView(QueuedTaskScheduler scheduler)
 {
     if (scheduler == null)
     {
         throw new ArgumentNullException("scheduler");
     }
     _scheduler = scheduler;
 }
예제 #4
0
        public QueuedTaskScheduler(int threadCount, string threadName = "", bool useForegroundThreads = false, ThreadPriority threadPriority = 2, ApartmentState threadApartmentState = 1, int threadMaxStackSize = 0, Action threadInit = null, Action threadFinally = null)
        {
            QueuedTaskScheduler queuedTaskScheduler = this;

            if (threadCount < 0)
            {
                throw new ArgumentOutOfRangeException("concurrencyLevel");
            }
            if (threadCount != 0)
            {
                this._concurrencyLevel = threadCount;
            }
            else
            {
                this._concurrencyLevel = Environment.ProcessorCount;
            }
            this._blockingTaskQueue = new BlockingCollection <Task>();
            this._threads           = new Thread[threadCount];
            for (int i = 0; i < threadCount; i++)
            {
                Thread[] threadArray = this._threads;
                int      num         = i;
                Thread   thread      = new Thread(() => queuedTaskScheduler.ThreadBasedDispatchLoop(threadInit, threadFinally), threadMaxStackSize)
                {
                    Priority     = threadPriority,
                    IsBackground = !useForegroundThreads
                };
                threadArray[num] = thread;
                if (threadName != null)
                {
                    Thread   thread1  = this._threads[i];
                    object[] objArray = new object[] { threadName, " (", i, ")" };
                    thread1.Name = string.Concat(objArray);
                }
                this._threads[i].SetApartmentState(threadApartmentState);
            }
            Thread[] threadArray1 = this._threads;
            for (int j = 0; j < (int)threadArray1.Length; j++)
            {
                threadArray1[j].Start();
            }
        }
예제 #5
0
 /// <summary>Initializes the queue.</summary>
 /// <param name="priority">The priority associated with this queue.</param>
 /// <param name="pool">The scheduler with which this queue is associated.</param>
 internal QueuedTaskSchedulerQueue(int priority, QueuedTaskScheduler pool)
 {
     _priority  = priority;
     _pool      = pool;
     _workItems = new Queue <Task>();
 }
예제 #6
0
 /// <summary>Initializes the debug view.</summary>
 /// <param name="scheduler">The scheduler.</param>
 public QueuedTaskSchedulerDebugView(QueuedTaskScheduler scheduler)
 {
     _scheduler = scheduler ?? throw new ArgumentNullException("scheduler");
 }
 /// <summary>Initializes the debug view.</summary>
 /// <param name="scheduler">The scheduler.</param>
 public QueuedTaskSchedulerDebugView(QueuedTaskScheduler scheduler) =>