Created
June 27, 2014 03:56
-
-
Save Fhernd/bfa4c74da70c8ba665fe to your computer and use it in GitHub Desktop.
Demostración de los estados de un thread antes y después de ser abortado.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Threading; | |
namespace Recetas.Threading.Cap01 | |
{ | |
public sealed class VerificacionEstadoThread | |
{ | |
public static void Main() | |
{ | |
Thread nuevoThread = new Thread( | |
delegate() | |
{ | |
Console.WriteLine ("\nDentro del thread `nuevoThread`..."); | |
// Ciclo infinito... | |
// Será interrumpido por nuevoThread.Abort() en | |
// el thread Main: | |
while(true); | |
} | |
); | |
// El thread nuevoThread aún no se ha iniciado: | |
Console.WriteLine ("\n1 - Estado actual de `nuevoThread`: {0}", nuevoThread.ThreadState); | |
// Inicio del thread `nuevoThread`: | |
nuevoThread.Start(); | |
Thread.Sleep (1000); | |
// `nuevoThread` se haya en ejecución: | |
Console.WriteLine ("\n2 - Estado actual de `nuevoThread`: {0}", nuevoThread.ThreadState); | |
// Aborta la ejecución de `nuevoThread`: | |
nuevoThread.Abort(); | |
// Nuevo estado de `nuevoThread`: | |
Console.WriteLine ("\n3 - Estado actual de `nuevoThread`: {0}", nuevoThread.ThreadState); | |
// Intento de poner en espera el thread Main, | |
// sin embargo el `nuevoThread` ha sido abortado: | |
nuevoThread.Join (); | |
Console.WriteLine ("\n4 - Estado actual de `nuevoThread`: {0}\n", nuevoThread.ThreadState); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment