Permalink
Cannot retrieve contributors at this time
Fetching contributors…
| //<snippet4> | |
| using System; | |
| using System.Net; | |
| using System.Threading; | |
| class Example | |
| { | |
| static void Main() | |
| { | |
| CancellationTokenSource cts = new CancellationTokenSource(); | |
| StartWebRequest(cts.Token); | |
| // cancellation will cause the web | |
| // request to be cancelled | |
| cts.Cancel(); | |
| } | |
| static void StartWebRequest(CancellationToken token) | |
| { | |
| WebClient wc = new WebClient(); | |
| wc.DownloadStringCompleted += (s, e) => Console.WriteLine("Request completed."); | |
| // Cancellation on the token will | |
| // call CancelAsync on the WebClient. | |
| token.Register(() => | |
| { | |
| wc.CancelAsync(); | |
| Console.WriteLine("Request cancelled!"); | |
| }); | |
| Console.WriteLine("Starting request."); | |
| wc.DownloadStringAsync(new Uri("http://www.contoso.com")); | |
| } | |
| } | |
| //</snippet4> |