Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using the new Async/Await pattern with Windows Forms Designer in Visual Studio 2012

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?

like image 701
Woodrow Douglass Avatar asked Dec 17 '12 21:12

Woodrow Douglass


1 Answers

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...
     }
}
like image 63
Reed Copsey Avatar answered Sep 18 '22 07:09

Reed Copsey