Advertisement
fcamuso

Networking, video 120

Aug 21st, 2021
1,869
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.35 KB | None | 0 0
  1. using System;
  2. using System.Net;
  3. using System.Runtime.CompilerServices;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Networking_A
  8. {
  9.   class Program
  10.   {
  11.     static ManualResetEvent mre = new ManualResetEvent(false);
  12.    
  13.     static void FaiAltro()
  14.     {
  15.       for (int i = 0; i < 100; i++)
  16.         Console.WriteLine("Lavoro ...");
  17.  
  18.       //Thread.Sleep(25000);
  19.     }
  20.  
  21.     static async Task Scarica(WebClient client)
  22.     {
  23.       await client.DownloadFileTaskAsync("https://www.camuso.it/DA_CANCELLARE/clip.mp4", "clip.mp4");
  24.       mre.Set();
  25.      
  26.       Console.WriteLine("Download completato");
  27.     }
  28.  
  29.     static void Main(string[] args)
  30.     {
  31.       //Download di un file, senza reporting
  32.       WebClient client = new () { Proxy = null };
  33.       client.DownloadFile("https://www.camuso.it/index.asp", "index.asp");
  34.  
  35.       //try
  36.       //{
  37.       //  client.DownloadFile("https://www.camuso.it/cicli2.jpg", "cicli2.jpg");
  38.       //}
  39.       //catch (WebException e)
  40.       //{
  41.       //  Console.WriteLine("Errore 404: risorsa non trovata");
  42.       //}
  43.  
  44.       //scaricamento in asincrono con possibilità di abortire
  45.       var t =Scarica(client);
  46.       Console.WriteLine("Io intanto continuo...");
  47.      
  48.       Task task = Task.Factory.StartNew(() => FaiAltro());
  49.       task.Wait();
  50.  
  51.       mre.WaitOne();
  52.  
  53.     }
  54.   }
  55. }
  56.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement