Created
June 27, 2014 04:40
-
-
Save Fhernd/8c5a462f5470091e453a to your computer and use it in GitHub Desktop.
Demostración del proceso de pase información a un thread a punto de ser abortado con el método Thread.Abort(Object).
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.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 ("\nEl thread `threadNuevo` ha finalizado."); | |
Console.WriteLine ("\nEl 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 ("\nMensaje excepción: {0}", | |
(string)tae.ExceptionState); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment