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<T>
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<Button> tcs;
private readonly Button button;
public MyForm()
{
this.button = new Button() { Text = "Click me" };
this.Controls.Add( this.button );
this.button.Click += this.OnButtonClicked;
}
private void OnButtonClicked( Object sender, EventArgs e )
{
if( this.tcs == null ) return;
this.tcs.SetResult( (Button)sender );
}
internal async Task BatchLogicAsync()
{
this.tcs = new TaskCompletionSource<Button>();
ProgressMessage = "Batch Logic Starts";
Button clickedButton = await this.tcs.Task;
ProgressMessage = "Batch Logic Ends";
}
}