Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. using System;
  2. using System.Threading;
  3.  
  4. namespace Recetas.Multithreading.Cap01
  5. {
  6.     internal sealed class ImpresorNumeros
  7.     {
  8.         // Imprime los números del 0 al 9 introduciendo
  9.         // un retraso de 2 segundos por cada iteración
  10.         // del ciclo for:
  11.         private static void ImprimirNumerosConRetraso()
  12.         {
  13.             Console.WriteLine ("Inicio ejecución...");
  14.            
  15.             for (int i = 0; i < 10; ++i)
  16.             {
  17.                 // Retraso (pausa) de dos (2) segundos:
  18.                 Thread.Sleep (TimeSpan.FromSeconds (2));
  19.                 Console.WriteLine (i.ToString());
  20.             }
  21.         }
  22.        
  23.         public static void Main()
  24.         {
  25.             Console.WriteLine ("\nIniciando la aplicación...");
  26.            
  27.             Thread threadNuevo = new Thread (ImprimirNumerosConRetraso);
  28.            
  29.             // Inicio del thread:
  30.             threadNuevo.Start();
  31.            
  32.             // Ponemos en espera el thread Main hasta que
  33.             // el thread threadNuevo termine:
  34.             threadNuevo.Join();
  35.            
  36.             Console.WriteLine ("\nThread Main terminó.\n");
  37.         }
  38.     }
  39. }