1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| public class AsyncSampleForm
{
private async void DoTheJob_Click ()
{
contentOutput.CLear ();
if (taskMethodChoice.Checked)
contentOutput.Text = await RetrieveContentTaskMethod (addressInput.Text);
else if (AsyncMethodChoice.Checked)
contentOutput.Text = await RetrieveContentAsyncMethod (addressInput.Text);
else
contentOutput.Text = await Task.Run (() => RetrieveContent (addressInput.Text));
}
private Task<string> RetrieveContentTaskMethod (string address)
{
using (var client = new Net.WebClient ())
{
return Task.Run (() => client.DownloadString (address));
}
}
private async Task<string> RetrieveContentAsyncMethod (string address)
{
using (var client = new Net.WebClient ())
{
return await client.DownloadStringTaskAsync (address);
}
}
private string RetrieveContent (string address)
{
using (var client = new Net.WebClient ())
{
return client.DownloadString (address);
}
}
} |