async and await key word is use for asynchronosly programming.
async and await does not work on multi threading concepts , they use only one thread through out hole application.
please see below ex i have used
private async void DownloadPageCount()
{
Task<string> getWebPageTask = GetWebPageAsync("http://msdn.microsoft.com");
Console.WriteLine("In DownloadPageCount before await");
string webText = await getWebPageTask;
Console.WriteLine("Characters received: " + webText.Length.ToString());
}
private async Task<string> GetWebPageAsync(string url)
{
var wc = new System.Net.WebClient();
Task<string> getStringTask = wc.DownloadStringTaskAsync(url); // its will return string as a task.
Console.WriteLine("In GetWebPageAsync before await");
string Text = await getStringTask;
Console.WriteLine("In GetWebPageAsync after await");
return Text;
}
private async void DownloadPageCount()
{
Task<string> getWebPageTask = GetWebPageAsync("http://msdn.microsoft.com");
Console.WriteLine("In DownloadPageCount before await");
string webText = await getWebPageTask;
Console.WriteLine("Characters received: " + webText.Length.ToString());
}
private async Task<string> GetWebPageAsync(string url)
{
var wc = new System.Net.WebClient();
Task<string> getStringTask = wc.DownloadStringTaskAsync(url); // its will return string as a task.
Console.WriteLine("In GetWebPageAsync before await");
string Text = await getStringTask;
Console.WriteLine("In GetWebPageAsync after await");
return Text;
}
When i call GetWebPageAsync() , so it will come inside that function and its will start downloading page , when he will come down to fourth line await getStringTask
await key going to stay while downloading complete, while in this period await keyword going to UI thread for that moment and he will continue with other process.
Note => So if use async and await keyword , so its does not speed up you program but its make it easy to work .
like i am showing million on data on page that time we see our UI thread all way block until data will popup on screen , so by using async and await we can handle that problem.
If we can print our thread id , we can its only one signal thread through out hole application.
Another ex of async and await
public async Task checkfinalresult()
{
Task<int> result = longruningprocess();
int result1 = await result;
Console.WriteLine("Final result ={0}",result1);
}
public async Task<int> longruningprocess()
{
await Task.Delay(1000); // Here he want wait to complicate process means while he free UI thread .
return 1;
}
Note => behind the seen he is doing lot of thing , if you want see you can use reflector to debug Microsoft ddl.
or you can use by changing debug option in VS 2013.
await key going to stay while downloading complete, while in this period await keyword going to UI thread for that moment and he will continue with other process.
Note => So if use async and await keyword , so its does not speed up you program but its make it easy to work .
like i am showing million on data on page that time we see our UI thread all way block until data will popup on screen , so by using async and await we can handle that problem.
If we can print our thread id , we can its only one signal thread through out hole application.
Another ex of async and await
public async Task checkfinalresult()
{
Task<int> result = longruningprocess();
int result1 = await result;
Console.WriteLine("Final result ={0}",result1);
}
public async Task<int> longruningprocess()
{
await Task.Delay(1000); // Here he want wait to complicate process means while he free UI thread .
return 1;
}
Note => behind the seen he is doing lot of thing , if you want see you can use reflector to debug Microsoft ddl.
or you can use by changing debug option in VS 2013.
No comments:
Post a Comment