public virtual int CompareTo(IComparableTask other)
        {
            if (other == null)
            {
                throw new ArgumentNullException(nameof(other));
            }

            return(this.CompareToByPriority(other as ComparableTask));
        }
        private async Task ExecuteComparableTaskAsync(IComparableTask comparableTask)
        {
            await this.canRunTaskSemaphoreSlim.WaitAsync(this.CancellationToken);

            // Using Task.Run to force this task to start on another thread.
#pragma warning disable 4014
            Task.Run(() => comparableTask.StartAsync(this.CancellationToken).ContinueWith((antecendent) =>
            {
                this.canRunTaskSemaphoreSlim.Release();
            }));
#pragma warning restore 4014
        }
Exemple #3
0
        private async Task QueueDelayedTaskAsync(IComparableTask comparableTask, TimeSpan delay)
        {
            if (this.delayedTasks.TryRemove(comparableTask, out Task task) && !task.IsCanceled)
            {
                if (delay > default(TimeSpan))
                {
                    await Task.Delay(delay, this.CancellationToken);
                }

                if (this.taskQueue.TryPeek(out IComparableTask firstComparableTask) && (comparableTask.CompareTo(firstComparableTask) <= 0))
                {
                    await this.ExecuteComparableTaskAsync(comparableTask);
                }
                else
                {
                    await this.taskQueue.AddAsync(comparableTask, this.CancellationToken);
                }
            }
        private async Task ExecuteComparableTaskAsync(IComparableTask comparableTask)
        {
            await this.canRunTaskSemaphoreSlim.WaitAsync(this.CancellationToken);

#pragma warning disable 4014
            // Schedule execution on current .NET task scheduler.
            // Compute gateway uses custom task scheduler to track tenant resource utilization.
            // Task.Run() switches to default task scheduler for entire sub-tree of tasks making compute gateway incapable of tracking resource usage accurately.
            // Task.Factory.StartNew() allows specifying task scheduler to use.
            Task.Factory.StartNewOnCurrentTaskSchedulerAsync(() =>
                                                             comparableTask.StartAsync(this.CancellationToken)
                                                             .ContinueWith((antecendent) =>
            {
                this.canRunTaskSemaphoreSlim.Release();
            },
                                                                           TaskScheduler.Current),
                                                             this.CancellationToken);
#pragma warning restore 4014
        }
        public bool TryQueueTask(IComparableTask comparableTask, TimeSpan delay = default(TimeSpan))
        {
            if (comparableTask == null)
            {
                throw new ArgumentNullException("task");
            }

            if (this.isStopped)
            {
                return(false);
            }

            Task newTask = new Task <Task>(() => this.QueueDelayedTaskAsync(comparableTask, delay), this.CancellationToken);

            if (this.delayedTasks.TryAdd(comparableTask, newTask))
            {
                newTask.Start();
                return(true);
            }

            return(false);
        }
 /// <summary>
 /// Determines whether this class is equal to another task.
 /// </summary>
 /// <param name="other">The other task</param>
 /// <returns>Whether this class is equal to another task.</returns>
 public override bool Equals(IComparableTask other)
 {
     return(this.Equals(other as ItemProducerTreeComparableTask));
 }
 public abstract bool Equals(IComparableTask other);
Exemple #8
0
 public override bool Equals(IComparableTask other)
 {
     return(this.CompareTo(other) == 0);
 }
 public override bool Equals(IComparableTask other)
 {
     return(this.Equals(other as DocumentProducerComparableTask));
 }