private void taskAdded(ThreadSafeSwitch taskSwitch)
            {
                if (m_stopCts.IsCancellationRequested)
                {
                    throw new InvalidOperationException("Could not add task - dispose is in progress");
                }

                taskSwitch.TrySet();
                isTaskLoopRequired();
            }
Esempio n. 2
0
        private TryAddTaskResult tryProcessTask(Task task, bool taskWasPreviouslyQueued)
        {
            var exceptionFromInnerTaskSchedulerRaised = false;
            var lockTaken = false;

            if (!isOriginalSchedulerInImplicitStrand())
            {
                lockTaken = m_canExecuteTaskSwitch.TrySet();

                if (!lockTaken)
                {
                    return(TryAddTaskResult.Rejected);
                }
            }


            var canExecuteTask = false;


            try
            {
                canExecuteTask = m_alreadyQueuedTasksTable.GetOrCreateValue(task).TrySet();
                if (!canExecuteTask)
                {
                    return(TryAddTaskResult.AlreadyAdded);
                }

                addTaskContinuation(task, taskWasPreviouslyQueued, lockTaken);
                return(executeTaskOnInnerScheduler(task));
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex);
                exceptionFromInnerTaskSchedulerRaised = true;
                throw;
            }
            finally
            {
                if ((exceptionFromInnerTaskSchedulerRaised || !canExecuteTask) && lockTaken)
                {
                    resetTaskLock();
                }
            }
        }
Esempio n. 3
0
        public virtual QueueTasksResult QueueTasksToInnerScheduler(QueueTasksParams queueTasksParams = null)
        {
            CheckIfDisposed();

            var currentParams = queueTasksParams ?? new QueueTasksParams();
            var currentTasks  = new List <Task>();

            var hasMoreTasks = false;

            try
            {
                if (processingCanceled())
                {
                    return(new QueueTasksResult(numberOfQueuedTasks: 0,
                                                whenAllTask: PredefinedTasks.CompletedTask,
                                                hasMoreTasks: false));
                }

                if (!m_queueToInnerSchedulerSwitch.TrySet())
                {
                    return(new QueueTasksResult(numberOfQueuedTasks: 0,
                                                whenAllTask: PredefinedTasks.CompletedTask,
                                                hasMoreTasks: !m_tasks.IsEmpty));
                }

                queueTasks(currentParams, currentTasks);

                hasMoreTasks = !m_tasks.IsEmpty;
            }
            finally
            {
                m_queueToInnerSchedulerSwitch.TryReset();
            }

            var whenAllTask = Task.WhenAll(currentTasks);
            var result      = new QueueTasksResult(numberOfQueuedTasks: currentTasks.Count,
                                                   whenAllTask: whenAllTask,
                                                   hasMoreTasks: hasMoreTasks);

            return(result);
        }