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


C# CancellationToken.ThrowIfSourceDisposed方法代码示例

本文整理汇总了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;
            }
        }
开发者ID:mesheets,项目名称:Theraot-CF,代码行数:52,代码来源:Task.microsoft.net35.cs


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