Advertisement
Fhernd

FinalizacionEjecucionThread.cs

Aug 13th, 2014
2,205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.85 KB | None | 0 0
  1. using System;
  2. using System.Threading;
  3.  
  4. namespace Recetas.CSharp.Cap04.R0412
  5. {
  6.     public sealed class FinalizacionEjecucionThread
  7.     {
  8.         public static void Main()
  9.         {
  10.             Console.Title = "--- Identificación Finalización Ejecución de un Thread ---";
  11.             Console.WriteLine ();
  12.        
  13.             // Creación Thread:
  14.             Thread t = new Thread (MostrarMensaje);
  15.            
  16.             // Iniciar ejecución:
  17.             t.Start();
  18.            
  19.             // Bloqueo hasta finalización del método MostrarMensaje, o
  20.             // hasta que transcurra un tiempo de espera (2 segundos):
  21.             if (!t.Join (2000))
  22.             {
  23.                 Console.WriteLine ("\nAgotado tiempo de espera de Join: {0}",
  24.                     DateTime.Now.ToString ("HH:mm:ss.ffff")
  25.                 );
  26.             }
  27.            
  28.             // Muestra en pantalla el estado del thread:
  29.             Console.WriteLine ("\n¿Thread en ejecución?: {0}", t.IsAlive.ToString());
  30.            
  31.             // Nuevo bloqueo hasta finalizar `MostrarMensaje`:
  32.             t.Join();
  33.            
  34.             // Imprime el estado actual del thread:
  35.             Console.WriteLine ("\n¿Thread en ejecución?: {0}", t.IsAlive.ToString());
  36.            
  37.             Console.WriteLine ("\nPresione Enter para finalizar.\n");
  38.             Console.ReadLine ();
  39.         }
  40.        
  41.         // Muestra mensajes de estado de ejecución en pantalla:
  42.         private static void MostrarMensaje()
  43.         {
  44.             for (int i = 1; i < 5; ++i)
  45.             {
  46.                 Console.WriteLine ("Registro de `MostrarMensaje`: {0}",
  47.                     DateTime.Now.ToString("HH:mm:ss.ffff")
  48.                 );
  49.                
  50.                 // Pausa por 1 segundo:
  51.                 Thread.Sleep (1000);
  52.             }
  53.         }
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement