using System;
using System.Threading ;
namespace Recetas.Multithreading .Cap01
{
internal sealed class UsoAbortObject
{
public static void Main( )
{
Console.WriteLine ( ) ;
Thread threadNuevo = new Thread ( EjecutarTarea) ;
threadNuevo.Start ( ) ;
Thread.Sleep ( 1000 ) ;
// Aborto del thread `threadNuevo`:
Console.WriteLine ( "Thread Main abortando el nuevo thread." ) ;
threadNuevo.Abort ( "Datos desde el thread Main." ) ;
// A espera a que el thread `threadNuevo` termine:
threadNuevo.Join ( ) ;
Console.WriteLine ( "\n El thread `threadNuevo` ha finalizado." ) ;
Console.WriteLine ( "\n El thread Main a punto de finalizar.\n " ) ;
}
private static void EjecutarTarea ( )
{
try
{
while ( true )
{
Console.WriteLine ( "`threadNuevo` en ejecución..." ) ;
Thread.Sleep ( 1000 ) ;
}
}
catch( ThreadAbortException tae)
{
Console.WriteLine ( "\n Mensaje excepción: {0}" ,
( string) tae.ExceptionState ) ;
}
}
}
}
dXNpbmcgU3lzdGVtOwp1c2luZyBTeXN0ZW0uVGhyZWFkaW5nOwoKbmFtZXNwYWNlIFJlY2V0YXMuTXVsdGl0aHJlYWRpbmcuQ2FwMDEKewogICAgaW50ZXJuYWwgc2VhbGVkIGNsYXNzIFVzb0Fib3J0T2JqZWN0CiAgICB7CiAgICAgICAgcHVibGljIHN0YXRpYyB2b2lkIE1haW4oKQogICAgICAgIHsKICAgICAgICAgICAgQ29uc29sZS5Xcml0ZUxpbmUoKTsKICAgICAgICAKICAgICAgICAgICAgVGhyZWFkIHRocmVhZE51ZXZvID0gbmV3IFRocmVhZCAoRWplY3V0YXJUYXJlYSk7CiAgICAgICAgICAgIHRocmVhZE51ZXZvLlN0YXJ0KCk7CiAgICAgICAgICAgIAogICAgICAgICAgICBUaHJlYWQuU2xlZXAgKDEwMDApOwogICAgICAgICAgICAKICAgICAgICAgICAgLy8gQWJvcnRvIGRlbCB0aHJlYWQgYHRocmVhZE51ZXZvYDoKICAgICAgICAgICAgQ29uc29sZS5Xcml0ZUxpbmUgKCJUaHJlYWQgTWFpbiBhYm9ydGFuZG8gZWwgbnVldm8gdGhyZWFkLiIpOwogICAgICAgICAgICB0aHJlYWROdWV2by5BYm9ydCggIkRhdG9zIGRlc2RlIGVsIHRocmVhZCBNYWluLiIpOwogICAgICAgICAgICAKICAgICAgICAgICAgLy8gQSBlc3BlcmEgYSBxdWUgZWwgdGhyZWFkIGB0aHJlYWROdWV2b2AgdGVybWluZToKICAgICAgICAgICAgdGhyZWFkTnVldm8uSm9pbiAoKTsKICAgICAgICAgICAgCiAgICAgICAgICAgIENvbnNvbGUuV3JpdGVMaW5lICgiXG5FbCB0aHJlYWQgYHRocmVhZE51ZXZvYCBoYSBmaW5hbGl6YWRvLiIpOwogICAgICAgICAgICBDb25zb2xlLldyaXRlTGluZSAoIlxuRWwgdGhyZWFkIE1haW4gYSBwdW50byBkZSBmaW5hbGl6YXIuXG4iKTsKICAgICAgICB9CiAgICAgICAgCiAgICAgICAgcHJpdmF0ZSBzdGF0aWMgdm9pZCBFamVjdXRhclRhcmVhICgpCiAgICAgICAgewogICAgICAgICAgICB0cnkKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgd2hpbGUgKHRydWUpCiAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgQ29uc29sZS5Xcml0ZUxpbmUgKCJgdGhyZWFkTnVldm9gIGVuIGVqZWN1Y2nDs24uLi4iKTsKICAgICAgICAgICAgICAgICAgICBUaHJlYWQuU2xlZXAgKDEwMDApOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgICAgIGNhdGNoKFRocmVhZEFib3J0RXhjZXB0aW9uIHRhZSkKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgQ29uc29sZS5Xcml0ZUxpbmUgKCJcbk1lbnNhamUgZXhjZXBjacOzbjogezB9IiwgCiAgICAgICAgICAgICAgICAoc3RyaW5nKXRhZS5FeGNlcHRpb25TdGF0ZSk7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICB9Cn0=