public async TaskGetDataAsync() { var result = await httpClient.GetAsync("http://example.com/data"); var content = await result.Content.ReadAsStringAsync(); return JsonConvert.DeserializeObject (content); } public void RunOperation() { GetDataAsync().ContinueWith(t => { if (t.IsFaulted) { // handle exception HandleException(t.Exception); } else { var myObj = t.Result; // do something with myObj } }, TaskContinuationOptions.ExecuteSynchronously); }
public async Task DownloadFileAsync(string url, string fileName, ActionIn this example, we are defining an AsyncContinuation delegate that will be used as a continuation for an async operation `DownloadFileAsync()`. We are also passing an action method as a parameter to the method that updates the progress of the file download. Package Library: This code example does not use any external package library.progressCallback) { using var client = new WebClient(); client.DownloadProgressChanged += (sender, args) => { progressCallback(args.ProgressPercentage); }; await client.DownloadFileTaskAsync(url, fileName); } public void RunOperation() { DownloadFileAsync("http://example.com/myfile.zip", "myfile.zip", percentage => { // update progress bar UI UpdateProgressBar(percentage); }).ContinueWith(t => { if (t.IsFaulted) { // handle exception HandleException(t.Exception); } else { // file download complete ShowSuccessMessage(); } }, TaskContinuationOptions.ExecuteSynchronously); }