using System; using System.Threading; namespace Recetas.Multithreading.Cap01 { internal sealed class UsoThreadJoinInt32 { // Declaración de threads: private static Thread threadMain, thread1, thread2; public static void Main() { threadMain = Thread.CurrentThread; thread1 = new Thread (ProcesoThread); // El nombre facilita su identificación: thread1.Name = "Thread1"; thread1.Start(); // Crea una instancia de Thread: thread2 = new Thread (ProcesoThread); // El nombre facilita su identificación: thread2.Name = "Thread2"; thread2.Start(); } // Este método es el encargado de llevar a cabo // la tarea de ejecución de threads: private static void ProcesoThread() { Console.WriteLine ("\nThread en curso: {0}", Thread.CurrentThread.Name); if (Thread.CurrentThread.Name.Equals ("Thread1") && thread2.ThreadState != ThreadState.Unstarted) { // Espera a por 2 segundos a que `thread2` termine: if (thread2.Join (2000)) { Console.WriteLine ("`thread2` ha terminado su ejecución."); } else { Console.WriteLine ("El tiempo de espera ha transcurrido el `thread1` continuará su ejecución."); } } // Pausa el thread por 4 segundos: Thread.Sleep (4000); Console.WriteLine ("\nThread en curso: {0}", Thread.CurrentThread.Name); Console.WriteLine ("`thread1`: {0}", thread1.ThreadState); Console.WriteLine ("`thread2`: {0}\n", thread2.ThreadState); } } }