I have an I/O bound method that I want to run asynchronously.
In the help docs it mentions that I should use async and await without Task.Run
to quote
For I/O-bound code, you await an operation which returns a Task or Task inside of an async method.
How do I do this from a winforms button click event?
I have tried
private void button_Click(object sender, EventArgs e)
{
await doLoadJob();
}
private async Task<int> doLoadJob()
{
await loadJob();
return 0;
}
STEP 01 Create new MVC Application project, named as "Async". In the File menu, click New Project. In the "New Project" dialog box, under Project types, expand Visual C#, and then click "Web". In the Name box, type "Async", then click on Ok.
The call to the async method starts an asynchronous task. However, because no Await operator is applied, the program continues without waiting for the task to complete. In most cases, that behavior isn't expected.
Your button_Click
method needs to be async
. Place a async
between private
and void
.
private async void button_Click(object sender, EventArgs e)
{
await LongOperation();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With