当前位置: 首页>>代码示例>>C#>>正文


C# Task.ThrowIfNull方法代码示例

本文整理汇总了C#中Task.ThrowIfNull方法的典型用法代码示例。如果您正苦于以下问题:C# Task.ThrowIfNull方法的具体用法?C# Task.ThrowIfNull怎么用?C# Task.ThrowIfNull使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Task的用法示例。


在下文中一共展示了Task.ThrowIfNull方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: QueueTask

        /// <summary>
        /// Queues a <see cref="Task"/> to the scheduler.
        /// </summary>
        /// <param name="task">The <see cref="Task"/> to be queued.</param>
        /// <exception cref="ArgumentNullException">The <paramref name="task"/> argument is null.</exception>
        protected override sealed void QueueTask(Task task)
        {
            task.ThrowIfNull("task");

            lock (_lockObject)
            {
                _completionTask = _completionTask ?? Task.Factory.StartNew(() => _manualResetEvent.Wait());
            }
            Interlocked.Increment(ref _taskCount);
            ThreadPool.UnsafeQueueUserWorkItem(
                state =>
                {
                    // Note that the current thread is now processing work items.
                    // This is necessary to enable inlining of tasks into this thread.
                    _currentThreadIsProcessingItems = true;
                    try
                    {
                        TryExecuteTaskWithCompletionTracking(task);
                    }
                    finally
                    {
                        // We're done processing items on the current thread
                        _currentThreadIsProcessingItems = false;
                    }
                },
                null);
        }
开发者ID:nathan-alden,项目名称:junior-common,代码行数:32,代码来源:AwaitableTaskScheduler.cs

示例2: TryExecuteTaskWithCompletionTracking

        /// <summary>
        /// Attempts to execute the a task and track its completion.
        /// </summary>
        /// <param name="task">A task to execute.</param>
        /// <exception cref="ArgumentNullException">Thrown when <paramref name="task"/> is null.</exception>
        protected virtual void TryExecuteTaskWithCompletionTracking(Task task)
        {
            task.ThrowIfNull("task");

            if (!TryExecuteTask(task))
            {
                return;
            }

            Interlocked.Decrement(ref _taskCount);
            if (_taskCount == 0)
            {
                _manualResetEvent.Set();
            }
        }
开发者ID:nathan-alden,项目名称:junior-common,代码行数:20,代码来源:AwaitableTaskScheduler.cs

示例3: TryExecuteTaskInline

        /// <summary>
        /// Determines whether the provided <see cref="Task"/> can be executed synchronously in this call, and if it can, executes it.
        /// </summary>
        /// <returns>
        /// A Boolean value indicating whether the task was executed inline.
        /// </returns>
        /// <param name="task">The <see cref="Task"/> to be executed.</param>
        /// <param name="taskWasPreviouslyQueued">A Boolean denoting whether or not task has previously been queued. If this parameter is True, then the task may have been previously queued (scheduled); if False, then the task is known not to have been queued, and this call is being made in order to execute the task inline without queuing it.</param>
        /// <exception cref="ArgumentNullException">The <paramref name="task"/> argument is null.</exception>
        /// <exception cref="InvalidOperationException">The <paramref name="task"/> was already executed.</exception>
        protected override bool TryExecuteTaskInline(Task task, bool taskWasPreviouslyQueued)
        {
            task.ThrowIfNull("task");

            // If this thread isn't already processing a task, we don't support inlining
            if (!_currentThreadIsProcessingItems)
            {
                return false;
            }

            // If the task was previously queued, remove it from the queue
            if (taskWasPreviouslyQueued)
            {
                TryDequeue(task);
            }

            // Try to run the task.
            return TryExecuteTask(task);
        }
开发者ID:nathan-alden,项目名称:junior-common,代码行数:29,代码来源:AwaitableTaskScheduler.cs


注:本文中的Task.ThrowIfNull方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。