问题
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;
}
回答1:
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();
}
来源:https://stackoverflow.com/questions/51664291/how-do-i-call-an-async-method-from-a-winforms-button-click-event