本文整理汇总了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);
}
示例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();
}
}
示例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);
}