TaskCompletionSource - Trying to understand threadless async work

后端 未结 2 1249
孤街浪徒
孤街浪徒 2020-12-22 21:35

I\'m trying to understand the purpose of TaskCompletionSource and its relation to async/threadless work. I think I have the general idea but I want to make sure

2条回答
  •  没有蜡笔的小新
    2020-12-22 21:43

    TaskCompletionSource is used to create Task objects that don't execute code.

    They're used quite a bit by Microsoft's new async APIs - any time there's I/O-based asynchronous operations (or other non-CPU-based asynchronous operations, like a timeout). Also, any async Task method you write will use TCS to complete its returned Task.

    I have a blog post Creating Tasks that discusses different ways to create Task instances. It's written from an async/await perspective (not a TPL perspective), but it still applies here.

    Also see Stephen Toub's excellent posts:

    • The Nature of TaskCompletionSource
    • Mechanisms for Creating Tasks
    • await anything; (using TaskCompletionSource to await anything).
    • Using Tasks to implement the APM Pattern (creating Begin/End using TaskCompletionSource).

提交回复
热议问题