Created
June 27, 2014 04:57
-
-
Save Fhernd/2153cdb0ee9fa9dfe192 to your computer and use it in GitHub Desktop.
Proceso de aborto de un thread en C#.
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 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