Skip to content

Instantly share code, notes, and snippets.

@Fhernd
Created June 27, 2014 04:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Fhernd/8c5a462f5470091e453a to your computer and use it in GitHub Desktop.
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).
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