Asynchronous wait for comparison method

I start with TPL and ask about the importance of invoking a method marked async in a call, or just waiting for a calling function that calls a method that is not defined as async.

private async void Button_Click_1(object sender, RoutedEventArgs e) { TBox.Text += await WebClientDownloader(); TBox.Text += await WebClientDownloadWithAwait(); } private async static Task<string> WebClientDownloadWithAwait() { using (var wc = new WebClient()) { return await wc.DownloadStringTaskAsync("http://google.com"); } } private static Task<string> WebClientDownloader() { using (var wc = new WebClient()) { return wc.DownloadStringTaskAsync("http://google.com"); } } 

Is there any difference? They seem to perform the same way.

+4
source share
1 answer

The difference is when Dispose() called. If you are not using await , then WebClient will Dispose() d immediately after starting the download and until the download is complete. It may work in your particular case, but it is not guaranteed to work, so you should definitely use await here.

+4
source

All Articles