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
toawait
anything). - Using Tasks to implement the APM Pattern (creating
Begin
/End
usingTaskCompletionSource
).