using System; using System.Threading; namespace Recetas.Multithreading.Cap01 { public sealed class UsoPropiedadThreadState { public static void Main() { Thread threadNuevo = new Thread (new ThreadStart (ImprimirNumerosConRetraso)); // Antes de empezar la ejecución del thread, este es su estado: Console.WriteLine ("\nEstado antes de empezar la ejecución el thread: `{0}`.", threadNuevo.ThreadState.ToString()); // Inicio de la ejecución del thread: threadNuevo.Start(); // Aborta el thread: threadNuevo.Abort(); // Nuevo estado del thread: Console.WriteLine ("\nNuevo estado del thread: `{0}`.", threadNuevo.ThreadState.ToString()); // Vuelve a iniciar el thread: threadNuevo = new Thread (ImprimirNumerosConRetraso); threadNuevo.Start(); // Espera a que la ejecución del thread `threadNuevo` finalice: threadNuevo.Join(); // Después de finalizar la ejecución del thread, este es su nuevo estado: Console.WriteLine ("\nEstado del thread `nuevoThread` después de finalizar su ejecución: `{0}`.", threadNuevo.ThreadState.ToString()); } private static void ImprimirNumerosConRetraso() { Console.WriteLine( "\nEstado actual del thread `nuevoThread`: `{0}`.\n", Thread.CurrentThread.ThreadState.ToString()); for (int i = 0; i < 9; ++i) { Thread.Sleep (1000); Console.WriteLine ("Valor actual de `i`: {0}", i.ToString()); } } } }