Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- // Add a using directive and a reference for System.Net.Http.
- using System.Net.Http;
- // Add the following using directive for System.Threading.
- using System.Threading;
- namespace CancelATask
- {
- public partial class MainWindow : Window
- {
- // ***Declare a System.Threading.CancellationTokenSource.
- CancellationTokenSource cts;
- public MainWindow()
- {
- InitializeComponent();
- }
- private async void startButton_Click(object sender, RoutedEventArgs e)
- {
- // ***Instantiate the CancellationTokenSource.
- cts = new CancellationTokenSource();
- resultsTextBox.Clear();
- try
- {
- // ***Send a token to carry the message if cancellation is requested.
- int contentLength = await AccessTheWebAsync(cts.Token);
- resultsTextBox.Text +=
- String.Format("\r\nLength of the downloaded string: {0}.\r\n", contentLength);
- }
- // *** If cancellation is requested, an OperationCanceledException results.
- catch (OperationCanceledException)
- {
- resultsTextBox.Text += "\r\nDownload canceled.\r\n";
- }
- catch (Exception)
- {
- resultsTextBox.Text += "\r\nDownload failed.\r\n";
- }
- // ***Set the CancellationTokenSource to null when the download is complete.
- cts = null;
- }
- // ***Add an event handler for the Cancel button.
- private void cancelButton_Click(object sender, RoutedEventArgs e)
- {
- if (cts != null)
- {
- cts.Cancel();
- }
- }
- // ***Provide a parameter for the CancellationToken.
- async Task<int> AccessTheWebAsync(CancellationToken ct)
- {
- HttpClient client = new HttpClient();
- resultsTextBox.Text +=
- String.Format("\r\nReady to download.\r\n");
- // You might need to slow things down to have a chance to cancel.
- await Task.Delay(250);
- // GetAsync returns a Task<HttpResponseMessage>.
- // ***The ct argument carries the message if the Cancel button is chosen.
- HttpResponseMessage response = await client.GetAsync("https://www.google.com/", ct);
- // Retrieve the website contents from the HttpResponseMessage.
- byte[] urlContents = await response.Content.ReadAsByteArrayAsync();
- // The result of the method is the length of the downloaded web site.
- return urlContents.Length;
- }
- }
- // Output for a successful download:
- // Ready to download.
- // Length of the downloaded string: 158125.
- // Or, if you cancel:
- // Ready to download.
- // Download canceled.
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement