Two new key words are used for Async feature: async modifier and await operator. Method marked
with async modifier is called async method. This new feature will help us a lot in async programming.
For example, in the programming of Winform, the UI thread will be blocked while we use HttpWebRequest synchronously request any resource in the Internet. From the perspective of user experience, we cannot interact with the form before the request is done.
with async modifier is called async method. This new feature will help us a lot in async programming.
For example, in the programming of Winform, the UI thread will be blocked while we use HttpWebRequest synchronously request any resource in the Internet. From the perspective of user experience, we cannot interact with the form before the request is done.
private void btnTest_Click(object sender, EventArgs e)
{
var request = WebRequest.Create(txtUrl.Text.Trim());
var content=new MemoryStream();
{
var request = WebRequest.Create(txtUrl.Text.Trim());
var content=new MemoryStream();
using (var response = request.GetResponse())
{
using (var responseStream = response.GetResponseStream())
{
responseStream.CopyTo(content);
}
}
txtResult.Text = content.Length.ToString();
}
{
using (var responseStream = response.GetResponseStream())
{
responseStream.CopyTo(content);
}
}
txtResult.Text = content.Length.ToString();
}
In the above example, after we clicked the Test button, we cannot not make any change to the form
before the txtResult textbox shows the result.
before the txtResult textbox shows the result.
In the past, we can also use BeginGetResponse method to send async request as this sample in MSDN
shows:
shows:
But it will takes us a lot effort to realize it. Now, we can simply use below code to do request asynchronously :
private async void btnTest_Click(object sender, EventArgs e)
{
var request = WebRequest.Create(txtUrl.Text.Trim());
var content = new MemoryStream();
Task<WebResponse> responseTask = request.GetResponseAsync();
using (var response = await responseTask)
{
{
var request = WebRequest.Create(txtUrl.Text.Trim());
var content = new MemoryStream();
Task<WebResponse> responseTask = request.GetResponseAsync();
using (var response = await responseTask)
{
using (var
responseStream = response.GetResponseStream())
{
Task copyTask = responseStream.CopyToAsync(content);
//await operator to supends the excution of the method until the task is completed. In the meantime, the control is returned the UI thread.
await copyTask;
}
}
txtResult.Text = content.Length.ToString();
}
responseStream = response.GetResponseStream())
{
Task copyTask = responseStream.CopyToAsync(content);
//await operator to supends the excution of the method until the task is completed. In the meantime, the control is returned the UI thread.
await copyTask;
}
}
txtResult.Text = content.Length.ToString();
}
The await operator is applied to the returned task. The await operator suspends execution of the method until the task is completed. Meanwhile, control is returned to the caller of the suspended method.