Advertisement
fcamuso

Threads - D/1

Jun 20th, 2021
1,181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.31 KB | None | 0 0
  1. using System;
  2. using System.Threading;
  3.  
  4. namespace threads_share_pass_return_B
  5. {
  6.   class Program
  7.   {
  8.     static int fattoriale = 0;
  9.  
  10.     class Punto
  11.     {
  12.       public int X { get; set; }
  13.       public int Y { get; set; }
  14.       public Punto(int x, int y) { X = x; Y = y; }
  15.     }
  16.  
  17.     static void Metodo(Object obj)
  18.     {
  19.       Punto p = (Punto)obj;
  20.       Console.WriteLine(p.X + p.Y);
  21.     }
  22.  
  23.     static void Fattoriale(Object obj)
  24.     {
  25.       int n = (int)obj;
  26.      
  27.       int risultato = n==0 ? 1 : n;
  28.  
  29.       while (n > 1)
  30.         risultato *= --n;
  31.  
  32.       fattoriale = risultato;
  33.     }
  34.  
  35.     static int MetodoPerLambda(int a, int b)
  36.     {
  37.       int risultato;
  38.       try
  39.       {
  40.         return a + b/0;
  41.       }
  42.       catch (DivideByZeroException err) {
  43.         Console.WriteLine($"Divisione per zero nel thread");
  44.       }
  45.      
  46.       return -1;
  47.  
  48.     }
  49.  
  50.     static void Main(string[] args)
  51.     {
  52.       Thread t1 = new Thread(Fattoriale);
  53.       t1.Start(5);
  54.       t1.Join();
  55.       Console.WriteLine(Program.fattoriale);
  56.  
  57.       int risultato = 0;
  58.       Thread t2 = new Thread( () => risultato = MetodoPerLambda(10, 20) );
  59.  
  60.       try
  61.       {
  62.         t2.Start();
  63.         t2.Join();
  64.       }
  65.       catch (DivideByZeroException err) { }
  66.       Console.WriteLine(risultato);
  67.  
  68.     }
  69.   }
  70. }
  71.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement