コード例 #1
0
        /// <summary>
        /// Execute all the work that was 'scheduled' by the tasks running on this runner.
        /// </summary>
        public void Execute()
        {
            if (this.isDisposed)
            {
                throw new ObjectDisposedException(nameof(LocalTaskRunner));
            }

            try
            {
                // Execute all the work that was scheduled on this runner.
                using (var contextScope = ContextScope.WithContext(this.context))
                {
                    this.context.Execute();
                }
            }
            finally
            {
                // Remove any tasks that are now finished.
                lock (this.runningTasksLock)
                {
                    for (int i = this.runningTasks.Count - 1; i >= 0; i--)
                    {
                        if (this.runningTasks[i].IsCompleted)
                        {
                            this.runningTasks.RemoveAt(i);
                        }
                    }
                }
            }
        }
コード例 #2
0
        /// <inheritdoc cref="ITaskRunner.StartTask(Func{CancellationToken, Task})"/>
        /// <param name="logger">Optional logger to output diagnostic messages to.</param>
        public Task StartTask(
            Func <CancellationToken, Task> taskCreator,
            IDiagnosticLogger logger)
        {
            if (taskCreator is null)
            {
                throw new ArgumentNullException(nameof(taskCreator));
            }
            if (this.isDisposed)
            {
                throw new ObjectDisposedException(nameof(LocalTaskRunner));
            }

            // Activate our context and wrap the task.
            using (var contextScope = ContextScope.WithContext(this.context))
            {
                var diagTracer = logger is null ? null : DiagTaskTracer.Create(logger, taskCreator);
                diagTracer?.LogInvoked();
                return(this.WrapTask(taskCreator.Invoke(this.cancelSource.Token), diagTracer));
            }
        }