Created
August 14, 2014 00:43
-
-
Save Fhernd/2e310b49b8b499aa5620 to your computer and use it in GitHub Desktop.
Demostración de la identificación de finalización de un thread.
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.CSharp.Cap04.R0412 | |
{ | |
public sealed class FinalizacionEjecucionThread | |
{ | |
public static void Main() | |
{ | |
Console.Title = "--- Identificación Finalización Ejecución de un Thread ---"; | |
Console.WriteLine (); | |
// Creación Thread: | |
Thread t = new Thread (MostrarMensaje); | |
// Iniciar ejecución: | |
t.Start(); | |
// Bloqueo hasta finalización del método MostrarMensaje, o | |
// hasta que transcurra un tiempo de espera (2 segundos): | |
if (!t.Join (2000)) | |
{ | |
Console.WriteLine ("\nAgotado tiempo de espera de Join: {0}", | |
DateTime.Now.ToString ("HH:mm:ss.ffff") | |
); | |
} | |
// Muestra en pantalla el estado del thread: | |
Console.WriteLine ("\n¿Thread en ejecución?: {0}", t.IsAlive.ToString()); | |
// Nuevo bloqueo hasta finalizar `MostrarMensaje`: | |
t.Join(); | |
// Imprime el estado actual del thread: | |
Console.WriteLine ("\n¿Thread en ejecución?: {0}", t.IsAlive.ToString()); | |
Console.WriteLine ("\nPresione Enter para finalizar.\n"); | |
Console.ReadLine (); | |
} | |
// Muestra mensajes de estado de ejecución en pantalla: | |
private static void MostrarMensaje() | |
{ | |
for (int i = 1; i < 5; ++i) | |
{ | |
Console.WriteLine ("Registro de `MostrarMensaje`: {0}", | |
DateTime.Now.ToString("HH:mm:ss.ffff") | |
); | |
// Pausa por 1 segundo: | |
Thread.Sleep (1000); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment