本文整理汇总了C#中CancellationToken.ThrowIfSourceDisposed方法的典型用法代码示例。如果您正苦于以下问题:C# CancellationToken.ThrowIfSourceDisposed方法的具体用法?C# CancellationToken.ThrowIfSourceDisposed怎么用?C# CancellationToken.ThrowIfSourceDisposed使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CancellationToken
的用法示例。
在下文中一共展示了CancellationToken.ThrowIfSourceDisposed方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AssignCancellationToken
private void AssignCancellationToken(CancellationToken cancellationToken, Task antecedent, TaskContinuation continuation)
{
CancellationToken = cancellationToken;
try
{
cancellationToken.ThrowIfSourceDisposed();
// If an unstarted task has a valid CancellationToken that gets signalled while the task is still not queued
// we need to proactively cancel it, because it may never execute to transition itself.
// The only way to accomplish this is to register a callback on the CT.
// We exclude Promise tasks from this, because TaskCompletionSource needs to fully control the inner tasks's lifetime (i.e. not allow external cancellations)
if ((_internalOptions & (InternalTaskOptions.QueuedByRuntime | InternalTaskOptions.PromiseTask | InternalTaskOptions.LazyCancellation)) == 0)
{
if (cancellationToken.IsCancellationRequested)
{
// Fast path for an already-canceled cancellationToken
InternalCancel(false);
}
else
{
// Regular path for an uncanceled cancellationToken
CancellationTokenRegistration registration;
if (antecedent == null)
{
// if no antecedent was specified, use this task's reference as the cancellation state object
registration = cancellationToken.Register(_taskCancelCallback, this);
}
else
{
// If an antecedent was specified, pack this task, its antecedent and the TaskContinuation together as a tuple
// and use it as the cancellation state object. This will be unpacked in the cancellation callback so that
// antecedent.RemoveCancellation(continuation) can be invoked.
registration = cancellationToken.Register(_taskCancelCallback, new Tuple<Task, Task, TaskContinuation>(this, antecedent, continuation));
}
_cancellationRegistration = new StrongBox<CancellationTokenRegistration>(registration);
}
}
}
catch (Exception)
{
// If we have an exception related to our CancellationToken, then we need to subtract ourselves
// from our parent before throwing it.
if ((_parent != null)
&& ((_creationOptions & TaskCreationOptions.AttachedToParent) != 0)
&& ((_parent._creationOptions & TaskCreationOptions.DenyChildAttach) == 0)
)
{
_parent.DisregardChild();
}
throw;
}
}