How I can make this code run?
It cannot be done because the async Task
signature is not implemented for UI event handlers in .net. (make sure to read this post: Why do event handlers always have a return type of void?)
The general consensus is that, in general, a return value of an event handler has not a well defined purpose. Although IMO it would make sense to be able to fire off all the handlers async, but then again, that would be exactly the case if you wouldn't await the logic inside it.
Of course you could overcome the problem by putting the event logic in another class. Most of the time this is actual a good idea anyhow.
Note: do not use it like this, it's just to explain it
public Form1 : Form
{
public Form1()
{
...
this.button1.Click += new System.EventHandler(this.button1_Click);
}
private async void button1_Click(object sender, EventArgs e) // line A
{
await YourLogic(e.foo); //optional configure await.
}
private async Task YourLogic(your parameters)
{
// do stuff
}
}
See also:
Why do event handlers always have a return type of void?
async/await - when to return a Task vs void?
https://msdn.microsoft.com/en-us/magazine/jj991977.aspx
How do I await events in C#?
How to make form events to use benefits from async?
So, basically it already does, but check out this post:
How to 'await' raising an EventHandler event