To await
something, it needs to be a Task
(this is an oversimplification, but that's the important part) (C# can await
any object that exposes a TaskAwaiter GetAwaiter()
method, but 95% of the time C# developers will be consuming Task
/Task
objects)..
To create a Task
you use TaskCompletionSource
. This is how you can create a Task
representation around other conceptual "tasks", like the IAsyncResult
-style APIs, threads, overlapped IO, and so on.
Assuming this is WinForms, then do this:
class MyForm : Form
{
// Note that `TaskCompletionSource` must have a generic-type argument.
// If you don't care about the result then use a dummy null `Object` or 1-byte `Boolean` value.
private TaskCompletionSource