When async-await change thread?

后端 未结 1 1745
旧巷少年郎
旧巷少年郎 2021-01-16 01:33
  • When I call test1() method then ManagedThreadId will be changed after Delay(1).
  • When I call test2() method (instead of test1()) then ManagedThreadId will stay
相关标签:
1条回答
  • 2021-01-16 01:50

    test1 awaits Task.Delay(1), which isn't going to be completed at the time it goes to await it, meaning the rest of test1 needs to be scheduled as a continuation.

    For test2 you're awaiting Task.FromResult, which will always return an already completed Task. When you await an already completed task the method can just keep running on the current thread, without needing to schedule a continuation.

    0 讨论(0)
提交回复
热议问题