Does .NET resume an await continuation on a new different thread pool thread or reuse the thread from a previous resumption?

后端 未结 4 1046
礼貌的吻别
礼貌的吻别 2021-01-20 23:30

Does .NET resume an await continuation on a new different thread pool thread or reuse the thread from a previous resumption?

Let\'s image below C# code in a .NET Core co

4条回答
  •  时光说笑
    2021-01-20 23:56

    Does .NET resume an await continuation on a new different thread pool thread or reuse the thread from a previous resumption?

    Most of the time it will use the same thread, but this is not guaranteed. This answer to a question where the OP wants to force it to the same thread gives some details.

    Where the continuation is run is up to the TaskScheduler. I haven't looked, but I imagine it will run the continuation on the same thread just to avoid unnecessary overhead when working with the thread pool.

    Is there any possibility each continuation will resume on a different thread pool thread like below?

    Yes, there's a possibility, but probable that you won't see this, again because of the scheduler. You'd probably have to write your own to force a difference, and personally I don't know why you'd do that. (Not saying that was your intent).

提交回复
热议问题