Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.29 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Navigation;
  14. using System.Windows.Shapes;
  15.  
  16. // Add a using directive and a reference for System.Net.Http.
  17. using System.Net.Http;
  18.  
  19. // Add the following using directive for System.Threading.
  20. using System.Threading;
  21.  
  22. namespace CancelATask
  23. {
  24.     public partial class MainWindow : Window
  25.     {
  26.         // ***Declare a System.Threading.CancellationTokenSource.
  27.         CancellationTokenSource cts;
  28.  
  29.         public MainWindow()
  30.         {
  31.             InitializeComponent();
  32.         }
  33.        
  34.  
  35.         private async void startButton_Click(object sender, RoutedEventArgs e)
  36.         {
  37.             // ***Instantiate the CancellationTokenSource.
  38.             cts = new CancellationTokenSource();
  39.  
  40.             resultsTextBox.Clear();
  41.  
  42.             try
  43.             {
  44.                 // ***Send a token to carry the message if cancellation is requested.
  45.                 int contentLength = await AccessTheWebAsync(cts.Token);
  46.                 resultsTextBox.Text +=
  47.                     String.Format("\r\nLength of the downloaded string: {0}.\r\n", contentLength);
  48.             }
  49.             // *** If cancellation is requested, an OperationCanceledException results.
  50.             catch (OperationCanceledException)
  51.             {
  52.                 resultsTextBox.Text += "\r\nDownload canceled.\r\n";
  53.             }
  54.             catch (Exception)
  55.             {
  56.                 resultsTextBox.Text += "\r\nDownload failed.\r\n";
  57.             }
  58.  
  59.             // ***Set the CancellationTokenSource to null when the download is complete.
  60.             cts = null;
  61.         }
  62.  
  63.  
  64.         // ***Add an event handler for the Cancel button.
  65.         private void cancelButton_Click(object sender, RoutedEventArgs e)
  66.         {
  67.             if (cts != null)
  68.             {
  69.                 cts.Cancel();
  70.             }
  71.         }
  72.  
  73.  
  74.         // ***Provide a parameter for the CancellationToken.
  75.         async Task<int> AccessTheWebAsync(CancellationToken ct)
  76.         {
  77.             HttpClient client = new HttpClient();
  78.  
  79.             resultsTextBox.Text +=
  80.                 String.Format("\r\nReady to download.\r\n");
  81.  
  82.             // You might need to slow things down to have a chance to cancel.
  83.             await Task.Delay(250);
  84.  
  85.             // GetAsync returns a Task<HttpResponseMessage>.
  86.             // ***The ct argument carries the message if the Cancel button is chosen.
  87.             HttpResponseMessage response = await client.GetAsync("https://www.google.com/", ct);
  88.  
  89.             // Retrieve the website contents from the HttpResponseMessage.
  90.             byte[] urlContents = await response.Content.ReadAsByteArrayAsync();
  91.  
  92.             // The result of the method is the length of the downloaded web site.
  93.             return urlContents.Length;
  94.         }
  95.     }
  96.  
  97.     // Output for a successful download:
  98.  
  99.     // Ready to download.
  100.  
  101.     // Length of the downloaded string: 158125.
  102.  
  103.  
  104.     // Or, if you cancel:
  105.  
  106.     // Ready to download.
  107.  
  108.     // Download canceled.
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement