获取任务的CancellationToken

问题描述 投票:21回答:7

我能得到CancellationToken这是通过执行任务操作时Task构造。大多数样品的这个样子:

CancellationTokenSource cts = new CancellationTokenSource();
CancellationToken token = cts.Token;

Task myTask = Task.Factory.StartNew(() =>
{
    for (...)
    {
        token.ThrowIfCancellationRequested();

        // Body of for loop.
    }
}, token);

但是,如果我的行为不是什么拉姆达但在方法放在其他类和我没有直接进入token?是唯一的办法是通过token的状态?

c# .net multithreading cancellation-token
7个回答
10
投票

但是,如果我的行为不是什么拉姆达但在方法放在其他类和我没有直接访问令牌?是唯一的办法是通过令牌状态?

是的,在这种情况下,你就需要通过令牌盒装的状态,或者包含在你的状态下使用一些其他类型。

这个,如果你打算使用的方法中的CancellationToken时,才需要的,但是。例如,如果你需要调用token.ThrowIfCancellationRequested()

如果你只使用令牌,以防止被调度的方法,那么它不是必需的。


12
投票

我能得到这是在执行任务过程中的行动传递给任务建构的CancellationToken?

不,你不能直接从Task对象得到它,没有。

但是,如果我的行为不是什么拉姆达但在方法放在其他类和我没有直接访问令牌?是唯一的办法是通过令牌状态?

这是两个选项,是的。还有其他的虽然。 (可能不是一个包容性的列表)。

  1. 您可以关闭在取消标记在一个匿名方法
  2. 你可以把它当作状态
  3. 您可以确保用于任务的委托实例具有保持到取消标记一个实例字段,或持有到一些对象持有到令牌等
  4. 您可以公开令牌而其他一些更大范围内的状态,即作为公共静态字段(在大多数情况下不好的做法,但它有可能适用)

8
投票

至于其他的答案中的状态,您可以通过令牌作为参数传递给你的方法。但是,要记住,你仍然想要将它传递给Task也很重要。 Task.Factory.StartNew( () => YourMethod(token), token),例如。

这保证了:

  1. 如果Task执行之前发生注销Task将无法运行(这是一个很好的优化)
  2. 通过调用的方法抛出的OperationCanceledException正确转换任务到Canceled状态

3
投票

有一个很简单的解决方案:

    class CancelingTasks
{
    private static void Foo(CancellationToken token)
    {
        while (true)
        {
            token.ThrowIfCancellationRequested();

            Thread.Sleep(100);
            Console.Write(".");                
        }
    }

    static void Main(string[] args)
    {
        CancellationTokenSource source = new CancellationTokenSource();
        CancellationToken tok = source.Token;

        tok.Register(() =>
        {
            Console.WriteLine("Cancelled.");
        });

        Task t = new Task(() =>
        {
            Foo(tok);
        }, tok);

        t.Start();

        Console.ReadKey();
        source.Cancel();
        source.Dispose();

        Console.WriteLine("Main program done, press any key.");
        Console.ReadKey();
    }
}

1
投票

您可以通过访问内部字段与反思得到的CancellationToken。

public CancellationToken GetCancellationToken(Task task)
{
    object m_contingentProperties = task
        .GetType()
        .GetField("m_contingentProperties",
            BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance)
        .GetValue(task);

    object m_cancellationToken = m_contingentProperties
        .GetType()
        .GetField("m_cancellationToken",
            BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance)
        .GetValue(m_contingentProperties);

    return (CancellationToken)m_cancellationToken;
}

提示:你可以搜索你自己这样的事情与ILSpy


0
投票

这似乎工作:

public static CancellationToken GetCancellationToken(this Task task)
{
  return new TaskCanceledException(task).CancellationToken;
}

这可能是必要的,使通用的任务助手保留已取消的任务的的CancellationToken(我来到这里,同时努力使Jon Skeet's WithAllExceptions method保存令牌)。


-1
投票

当我们看Task类参考源代码,我们可以看到,取消标记存储在一个内部类中:ContingentProperties

https://referencesource.microsoft.com/#mscorlib/system/threading/Tasks/Task.cs,90a9f91ddd80b5cc

其目的是为了避免这些属性的访问和这些性能并不总是必要的。

    internal class ContingentProperties
    {
        // Additional context

        internal ExecutionContext m_capturedContext; // The execution context to run the task within, if any.

        // Completion fields (exceptions and event)

        internal volatile ManualResetEventSlim m_completionEvent; // Lazily created if waiting is required.
        internal volatile TaskExceptionHolder m_exceptionsHolder; // Tracks exceptions, if any have occurred

        // Cancellation fields (token, registration, and internally requested)

        internal CancellationToken m_cancellationToken; // Task's cancellation token, if it has one
        internal Shared<CancellationTokenRegistration> m_cancellationRegistration; // Task's registration with the cancellation token
        internal volatile int m_internalCancellationRequested; // Its own field because threads legally ---- to set it.

        // Parenting fields

        // # of active children + 1 (for this task itself).
        // Used for ensuring all children are done before this task can complete
        // The extra count helps prevent the ---- for executing the final state transition
        // (i.e. whether the last child or this task itself should call FinishStageTwo())
        internal volatile int m_completionCountdown = 1;
        // A list of child tasks that threw an exception (TCEs don't count),
        // but haven't yet been waited on by the parent, lazily initialized.
        internal volatile List<Task> m_exceptionalChildren;

        /// <summary>
        /// Sets the internal completion event.
        /// </summary>
        internal void SetCompleted()
        {
            var mres = m_completionEvent;
            if (mres != null) mres.Set();
        }

        /// <summary>
        /// Checks if we registered a CT callback during construction, and deregisters it. 
        /// This should be called when we know the registration isn't useful anymore. Specifically from Finish() if the task has completed
        /// successfully or with an exception.
        /// </summary>
        internal void DeregisterCancellationCallback()
        {
            if (m_cancellationRegistration != null)
            {
                // Harden against ODEs thrown from disposing of the CTR.
                // Since the task has already been put into a final state by the time this
                // is called, all we can do here is suppress the exception.
                try { m_cancellationRegistration.Value.Dispose(); }
                catch (ObjectDisposedException) { }
                m_cancellationRegistration = null;
            }
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.