You need to put async in your method, I modified your code since the Click event signature does not return int, and your method AccessTheWebAsync
does, so I moved it to another method async that returns int, anyway I async and await are kind of syntactic sugar and is recommended that you take a look at what really happens to your code when you use these keywords, take a look here: http://www.codeproject.com/Articles/535635/Async-Await-and-the-Generated-StateMachine
private async void button1_Click(object sender, EventArgs e)
{
await ClickAsync();
}
private async Task AccessTheWebAsync()
{
return await Task.Run(() =>
{
Task.Delay(10000); //Some heavy work here
return 3; //replace with real result
});
}
public async Task ClickAsync()
{
int contentLength = await AccessTheWebAsync();
label1.Text = String.Format("\r\nLength of the downloaded string: {0}.\r\n", contentLength);
}
}