The WebClient class in C# provides a way to download data from and upload data to remote websites. The CancelAsync method of the WebClient class is used to cancel an asynchronous operation that is currently in progress.
Example 1: Downloading data In this example, we use the WebClient class to download data from a remote website. We call the DownloadStringAsync method to initiate the download, and then use the CancelAsync method to cancel the operation if it takes too long.
WebClient client = new WebClient(); client.DownloadStringCompleted += (s, e) => { if (e.Error != null || e.Cancelled) return; Console.WriteLine(e.Result); };
Task.Delay(1000).ContinueWith(t => { if (client.IsBusy) client.CancelAsync(); });
Example 2: Uploading data In this example, we use the WebClient class to upload data to a remote website. We call the UploadStringAsync method to initiate the upload, and then use the CancelAsync method to cancel the operation if it takes too long.
WebClient client = new WebClient(); client.UploadStringCompleted += (s, e) => { if (e.Error != null || e.Cancelled) return; Console.WriteLine(e.Result); };
Task.Delay(1000).ContinueWith(t => { if (client.IsBusy) client.CancelAsync(); });
Package/Library: System.Net.WebClient.
C# (CSharp) WebClient.CancelAsync - 33 examples found. These are the top rated real world C# (CSharp) examples of WebClient.CancelAsync extracted from open source projects. You can rate examples to help us improve the quality of examples.