Advertisement
fcamuso

TASK - A

Jun 29th, 2021
1,167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.85 KB | None | 0 0
  1. using System;
  2. using System.Threading;
  3. using System.Threading.Tasks;
  4.  
  5. namespace Task_A
  6. {
  7.   class Program
  8.   {
  9.  
  10.     static void MetodoGenerico()
  11.     {
  12.       for (int i = 0; i < 100; i++) Console.WriteLine(i);
  13.     }
  14.  
  15.     static int Sommatore(int[] v)
  16.     {
  17.       int somma = 0;
  18.       foreach (int n in v) { somma += n; Thread.Sleep(50); }
  19.       return somma;
  20.     }
  21.  
  22.     static void Main(string[] args)
  23.     {
  24.       int[] v = { 1, 2, 3, 4 };
  25.  
  26.       //Task t1 = Task.Run(() => MetodoGenerico());
  27.       //t1.Wait();
  28.  
  29.       Task<int> t2 = Task<int>.Run(() => Sommatore(v));
  30.       while (true)
  31.       {
  32.         Console.WriteLine($"Stato: {t2.Status}");
  33.         if (t2.Status == TaskStatus.RanToCompletion) break;
  34.         Thread.Sleep(50);
  35.       }
  36.  
  37.       Console.WriteLine(t2.Result);
  38.  
  39.  
  40.       Console.WriteLine("main terminato");
  41.     }
  42.   }
  43. }
  44.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement