I'm trying to implement async calls with the new Async/Await pattern in visual studio 2012. When i set up my form (using the form designer) and then try to use an async method as an event handler, the compiler complains that the function doesn't return void.
The method is supposed to return a Task; that's the whole point. I cant figure out how to tell the form designer that this isn't a regular event handler. Has anyone run into this issue? should i quit using the form designer for Rapid Development?
You have to use an async void
method for the event handler, instead of async Task
. Being able to wire up event handlers to async methods is the entire reason async void
is allowed.
For example, if you want to use a button click handler, you'd write it like:
private async void button_Click(object sender, EventArgs e)
{
bool success = await CallSomeMethodAsync();
if (success)
{
// Do something here, etc...
}
}
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