Advertisement
Guest User

Untitled

a guest
Apr 25th, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.50 KB | None | 0 0
  1. using System;
  2. using System.Net;
  3. using System.Threading;
  4. using System.Threading.Tasks;
  5.  
  6. namespace ConsoleApp3
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             CancellationTokenSource cts = new CancellationTokenSource();
  13.  
  14.             Call(cts.Token);
  15.  
  16.             for (int i = 0; i < 1000; i++)
  17.             {
  18.                 Thread.Sleep(20);
  19.                 if (i == 30)
  20.                     cts.Cancel();
  21.                 Console.WriteLine($"Do something else({i})");
  22.             }
  23.  
  24.             Console.ReadLine();
  25.         }
  26.  
  27.         static async void Call(CancellationToken cancellationToken)
  28.         {
  29.             try
  30.             {
  31.                 Downloader downloader = new Downloader();
  32.                 string res = await downloader.DownloadAsync("https://pokeapi.co/api/v2/pokemon/1/", cancellationToken);
  33.                 Console.WriteLine($"RESPONCE: {res}");
  34.             }
  35.             catch (OperationCanceledException)
  36.             {
  37.                 Console.WriteLine($"ERROR");
  38.             }
  39.         }
  40.     }
  41.  
  42.  
  43.     class Downloader
  44.     {
  45.         public Task<string> DownloadAsync(string url, CancellationToken cancellationToken)
  46.         {
  47.             TaskCompletionSource<string> tcs = new TaskCompletionSource<string>();
  48.  
  49.             using (var client = new WebClient())
  50.             {
  51.                 Uri Uri = new Uri(url);
  52.                 cancellationToken.Register(() => client.CancelAsync());
  53.                 client.DownloadStringCompleted += (object sender, DownloadStringCompletedEventArgs e) =>
  54.                 {
  55.                     if (e.Cancelled)
  56.                     {
  57.                         try
  58.                         {
  59.                             cancellationToken.ThrowIfCancellationRequested();
  60.                         }
  61.                         catch (Exception ex)
  62.                         {
  63.                             Console.WriteLine(ex.Message + "xui" + e.Error.Message);
  64.                             tcs.SetCanceled();
  65.                             return;
  66.                         }
  67.                     }
  68.                     //if (e.Error != null)
  69.                     //{
  70.                     //    Console.WriteLine("Can't set result" + e.Error.Message);
  71.                     //    return;
  72.                     //}
  73.                     tcs.SetResult(e.Result);
  74.                 };
  75.  
  76.                 client.DownloadStringAsync(Uri, cancellationToken);
  77.             }
  78.  
  79.             return tcs.Task;
  80.         }
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement