i am new to metro application development, i request you to help me understand the usage of async and await key words ,

As per my knowledge i understood that we async and await both simultaneously.

But if one method / function is mention as async :-

private async void Button_Click_1(object sender, RoutedEventArgs e)
{
    Uri inputUri  = new Uri("http://examplewebservices");

    try
    {

   string result =  await httpClient.GetStringAsync(inputUri);

///
i have to do some operations on this string result here 
///

    }
    catch (Exception ex)
    {

    }
}    

1)what happens will a separate thread is created ?, and every thing inside that runs asynchronously ?

2)What will await keyword do here ?

3)if it is asynchronous , there is place in the code where i need to perform some operations on string result , if that is not completed it would show a error right ?

Please guide me out.

Thanks in Advance.

有帮助吗?

解决方案

Please read my async/await intro.

1)what happens will a separate thread is created ?, and every thing inside that runs asynchronously ?

No. async methods are synchronous until they await an operation that isn't completed.

2)What will await keyword do here ?

It will asynchronously wait until the operation completes.

In this case, it will return back to the message loop and schedule the remainder of the method to run (on the UI thread) after the download completes.

3)if it is asynchronous , there is place in the code where i need to perform some operations on string result , if that is not completed it would show a error right ?

No. Because of the await, the rest of the method won't run until the download is complete.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top