Skip to content

Instantly share code, notes, and snippets.

@Fhernd
Created June 27, 2014 04:57
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/2153cdb0ee9fa9dfe192 to your computer and use it in GitHub Desktop.
Save Fhernd/2153cdb0ee9fa9dfe192 to your computer and use it in GitHub Desktop.
Proceso de aborto de un thread en C#.
using System;
using System.Threading;
namespace Recetas.Multithreading.Cap01
{
internal sealed class ImpresorNumeros
{
// Imprime los números del 0 al 9 introduciendo
// un retraso de 2 segundos por cada iteración
// del ciclo for:
private static void ImprimirNumerosConRetraso()
{
Console.WriteLine ("Inicio ejecución `ImprimirNumerosConRetraso`...");
for (int i = 0; i < 10; ++i)
{
// Retraso (pausa) de dos (2) segundos:
Thread.Sleep (TimeSpan.FromSeconds (2));
Console.WriteLine (i.ToString());
}
}
public static void Main()
{
Console.WriteLine ("\nInicio ejecución Main...");
// Creación Thread:
Thread nuevoThread = new Thread (ImprimirNumerosConRetraso);
// Inicio Thread:
nuevoThread.Start();
// Pausa el thread Main durante 6 segundos:
Thread.Sleep (TimeSpan.FromSeconds (6));
// Aborta el thread `nuevoThread`:
nuevoThread.Abort();
Console.WriteLine ("\nUn thread ha sido abortado.\n");
// Crea un nuevo thread:
Thread nuevoThread2 = new Thread(ImprimirNumerosConRetraso);
nuevoThread2.Start();
Console.WriteLine ("\nA punto de finalizar el thread Main\n");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment