Loading Lezione1_Thread/Program.cs +5 −20 Original line number Diff line number Diff line Loading @@ -15,8 +15,7 @@ public class Lezione1 { Console.Write($"{suono} "); // Simula il tempo di lavoro con un ritardo casuale (100-300 ms) Thread.Sleep(rand.Next(100, 300)); } Console.WriteLine($"\n--- FINE: Campana '{suono}' ---"); Loading @@ -24,28 +23,14 @@ public class Lezione1 public static void Main(string[] args) { // 1. Creazione dei thread, passando il metodo da eseguire Thread t_din = new Thread(() => SuonaCampana("DIN")); Thread t_don = new Thread(() => SuonaCampana("DON")); Thread t_dan = new Thread(() => SuonaCampana("DAN")); // OPZIONALE: Rendiamo i thread background t_din.IsBackground = true; t_don.IsBackground = true; t_dan.IsBackground = true; Console.WriteLine("Avvio delle campane in parallelo..."); // 2. Avvio dei percorsi di esecuzione paralleli t_din.Start(); t_don.Start(); t_dan.Start(); // 3. Blocco del thread principale (Main) in attesa che tutti finiscano // SENZA JOIN, il Main terminerebbe subito e i thread secondari si interromperebbero (se IsBackground è true) t_din.Join(); t_don.Join(); t_dan.Join(); Console.WriteLine("\n\n--- Tutti i thread sono terminati. Programma chiuso. ---"); } Loading Loading
Lezione1_Thread/Program.cs +5 −20 Original line number Diff line number Diff line Loading @@ -15,8 +15,7 @@ public class Lezione1 { Console.Write($"{suono} "); // Simula il tempo di lavoro con un ritardo casuale (100-300 ms) Thread.Sleep(rand.Next(100, 300)); } Console.WriteLine($"\n--- FINE: Campana '{suono}' ---"); Loading @@ -24,28 +23,14 @@ public class Lezione1 public static void Main(string[] args) { // 1. Creazione dei thread, passando il metodo da eseguire Thread t_din = new Thread(() => SuonaCampana("DIN")); Thread t_don = new Thread(() => SuonaCampana("DON")); Thread t_dan = new Thread(() => SuonaCampana("DAN")); // OPZIONALE: Rendiamo i thread background t_din.IsBackground = true; t_don.IsBackground = true; t_dan.IsBackground = true; Console.WriteLine("Avvio delle campane in parallelo..."); // 2. Avvio dei percorsi di esecuzione paralleli t_din.Start(); t_don.Start(); t_dan.Start(); // 3. Blocco del thread principale (Main) in attesa che tutti finiscano // SENZA JOIN, il Main terminerebbe subito e i thread secondari si interromperebbero (se IsBackground è true) t_din.Join(); t_don.Join(); t_dan.Join(); Console.WriteLine("\n\n--- Tutti i thread sono terminati. Programma chiuso. ---"); } Loading