December 2019
Intermediate to advanced
346 pages
9h 8m
English
This approach makes use of a Callback delegate that gets invoked when the cancellation is requested by the underlying token. We should use this with operations that are blocked in a way that makes it not possible to check the value of CancellationToken in a regular fashion.
Let's have a look at the following code, which downloads files from a remote URL:
private static void DownloadFileWithoutToken(){ WebClient webClient = new WebClient(); webClient.DownloadStringAsync(new Uri("http://www.google.com")); webClient.DownloadStringCompleted += (sender, e) => { if (!e.Cancelled) Console.WriteLine("Download Complete."); else Console.WriteLine("Download Cancelled."); };}
As you ...
Read now
Unlock full access