Task.ContinueWith callback thread

前端 未结 2 1513
一向
一向 2021-01-06 12:09

I tried to find an answer for this but couldn\'t. What I was wondering is that on which thread Task.ContinueWith delegate is called. For await I know that it tr

2条回答
  •  离开以前
    2021-01-06 12:46

    Task.ContinueWith is scheduled on the TaskScheduler.Current unless specified otherwise by the parameters in one of the optional overloads.

    If you don't have a custom scheduler in TaskScheduler.Current (which is very likely) your continuation will run on the ThreadPool.

    Task.ContinueWith never uses the SynchronizationContext unless you create a TaskScheduler out of it with TaskScheduler.FromCurrentSynchronizationContext.

    You can always state explicitly which TaskScheduler is needed using one of the available overloads:

    task.ContinueWith(
        _ => {}, 
        null, 
        CancellationToken.None, 
        TaskContinuationOptions.None, 
        TaskScheduler.Default); // Scheduled to the ThreadPool
    

提交回复
热议问题