When should Task.ContinueWith be called with TaskScheduler.Current as an argument?

前端 未结 4 460
悲&欢浪女
悲&欢浪女 2021-02-02 15:03

We are using this code snippet from StackOverflow to produce a Task that completes as soon as the first of a collection of tasks completes successfully. Due to the non-linear na

4条回答
  •  一整个雨季
    2021-02-02 15:29

    If current task is a child task, then using TaskScheduler.Current will mean the scheduler will be that which the task it is in, is scheduled to; and if not inside another task, TaskScheduler.Current will be TaskScheduler.Default and thus use the ThreadPool.

    If you use TaskScheduler.Default, then it will always go to the ThreadPool.

    The only reason you would use TaskScheduler.Current:

    To avoid the default scheduler issue, you should always pass an explicit TaskScheduler to Task.ContinueWith and Task.Factory.StartNew.

    From Stephen Cleary's post ContinueWith is Dangerous, Too.

    There's further explanation here from Stephen Toub on his MSDN blog.

提交回复
热议问题