using System;
using System.Threading ;
namespace Recetas.Threading .Cap01
{
public sealed class VerificacionEstadoThread
{
public static void Main( )
{
Thread nuevoThread = new Thread(
delegate( )
{
Console.WriteLine ( "\n Dentro 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 ( "\n 1 - Estado actual de `nuevoThread`: {0}" , nuevoThread.ThreadState ) ;
// Inicio del thread `nuevoThread`:
nuevoThread.Start ( ) ;
Thread.Sleep ( 1000 ) ;
// `nuevoThread` se haya en ejecución:
Console.WriteLine ( "\n 2 - Estado actual de `nuevoThread`: {0}" , nuevoThread.ThreadState ) ;
// Aborta la ejecución de `nuevoThread`:
nuevoThread.Abort ( ) ;
// Nuevo estado de `nuevoThread`:
Console.WriteLine ( "\n 3 - 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 ( "\n 4 - Estado actual de `nuevoThread`: {0}\n " , nuevoThread.ThreadState ) ;
}
}
}
dXNpbmcgU3lzdGVtOwp1c2luZyBTeXN0ZW0uVGhyZWFkaW5nOwoKbmFtZXNwYWNlIFJlY2V0YXMuVGhyZWFkaW5nLkNhcDAxCnsKICAgIHB1YmxpYyBzZWFsZWQgY2xhc3MgVmVyaWZpY2FjaW9uRXN0YWRvVGhyZWFkCiAgICB7CiAgICAgICAgcHVibGljIHN0YXRpYyB2b2lkIE1haW4oKQogICAgICAgIHsKICAgICAgICAgICAgVGhyZWFkIG51ZXZvVGhyZWFkID0gbmV3IFRocmVhZCgKICAgICAgICAgICAgICAgIGRlbGVnYXRlKCkKICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICBDb25zb2xlLldyaXRlTGluZSAoIlxuRGVudHJvIGRlbCB0aHJlYWQgYG51ZXZvVGhyZWFkYC4uLiIpOwogICAgICAgICAgICAgICAgICAgIC8vIENpY2xvIGluZmluaXRvLi4uCiAgICAgICAgICAgICAgICAgICAgLy8gU2Vyw6EgaW50ZXJydW1waWRvIHBvciBudWV2b1RocmVhZC5BYm9ydCgpIGVuIAogICAgICAgICAgICAgICAgICAgIC8vIGVsIHRocmVhZCBNYWluOgogICAgICAgICAgICAgICAgICAgIHdoaWxlKHRydWUpOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICApOwogICAgICAgICAgICAKICAgICAgICAgICAgLy8gRWwgdGhyZWFkIG51ZXZvVGhyZWFkIGHDum4gbm8gc2UgaGEgaW5pY2lhZG86CiAgICAgICAgICAgIENvbnNvbGUuV3JpdGVMaW5lICgiXG4xIC0gRXN0YWRvIGFjdHVhbCBkZSBgbnVldm9UaHJlYWRgOiB7MH0iLCBudWV2b1RocmVhZC5UaHJlYWRTdGF0ZSk7CiAgICAgICAgICAgIAogICAgICAgICAgICAvLyBJbmljaW8gZGVsIHRocmVhZCBgbnVldm9UaHJlYWRgOgogICAgICAgICAgICBudWV2b1RocmVhZC5TdGFydCgpOwogICAgICAgICAgICBUaHJlYWQuU2xlZXAgKDEwMDApOwogICAgICAgICAgICAKICAgICAgICAgICAgLy8gYG51ZXZvVGhyZWFkYCBzZSBoYXlhIGVuIGVqZWN1Y2nDs246CiAgICAgICAgICAgIENvbnNvbGUuV3JpdGVMaW5lICgiXG4yIC0gRXN0YWRvIGFjdHVhbCBkZSBgbnVldm9UaHJlYWRgOiB7MH0iLCBudWV2b1RocmVhZC5UaHJlYWRTdGF0ZSk7CiAgICAgICAgICAgIAogICAgICAgICAgICAvLyBBYm9ydGEgbGEgZWplY3VjacOzbiBkZSBgbnVldm9UaHJlYWRgOgogICAgICAgICAgICBudWV2b1RocmVhZC5BYm9ydCgpOwogICAgICAgICAgICAKICAgICAgICAgICAgLy8gTnVldm8gZXN0YWRvIGRlIGBudWV2b1RocmVhZGA6CiAgICAgICAgICAgIENvbnNvbGUuV3JpdGVMaW5lICgiXG4zIC0gRXN0YWRvIGFjdHVhbCBkZSBgbnVldm9UaHJlYWRgOiB7MH0iLCBudWV2b1RocmVhZC5UaHJlYWRTdGF0ZSk7CiAgICAgICAgICAgIAogICAgICAgICAgICAvLyBJbnRlbnRvIGRlIHBvbmVyIGVuIGVzcGVyYSBlbCB0aHJlYWQgTWFpbiwgCiAgICAgICAgICAgIC8vIHNpbiBlbWJhcmdvIGVsIGBudWV2b1RocmVhZGAgaGEgc2lkbyBhYm9ydGFkbzoKICAgICAgICAgICAgbnVldm9UaHJlYWQuSm9pbiAoKTsKICAgICAgICAgICAgCiAgICAgICAgICAgIENvbnNvbGUuV3JpdGVMaW5lICgiXG40IC0gRXN0YWRvIGFjdHVhbCBkZSBgbnVldm9UaHJlYWRgOiB7MH1cbiIsIG51ZXZvVGhyZWFkLlRocmVhZFN0YXRlKTsKICAgICAgICB9CiAgICB9Cn0=