Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. using System;
  2. using System.Threading;
  3.  
  4. namespace Recetas.CSharp.Cap04.R0407
  5. {
  6.     public class TicTac
  7.     {
  8.         public void Tic (bool enEjecucion)
  9.         {
  10.             lock (this)
  11.             {
  12.                 if (!enEjecucion)
  13.                 {
  14.                     // Notifica a los threads en espera:
  15.                     Monitor.Pulse (this);
  16.                     return;
  17.                 }
  18.                 Thread.Sleep (1000);
  19.                 Console.Write ("Tic ");
  20.                
  21.                 // Permite el paso de la ejecución del método Tac
  22.                 Monitor.Pulse (this);
  23.                
  24.                 // ...y espera a que el método Tac termine:
  25.                 Monitor.Wait (this);
  26.             }
  27.         }
  28.        
  29.         public void Tac (bool enEjecucion)
  30.         {
  31.             lock (this)
  32.             {
  33.                 if (!enEjecucion)
  34.                 {
  35.                     // Notifica a los threads en espera:
  36.                     Monitor.Pulse (this);
  37.                     return;
  38.                 }
  39.                 Thread.Sleep (1000);
  40.                 Console.WriteLine ("Tac");
  41.                
  42.                 // Permite el paso de la ejecución del método Tic
  43.                 Monitor.Pulse (this);
  44.                
  45.                 // ...y espera a que el método Tic termine:
  46.                 Monitor.Wait (this);
  47.             }
  48.         }
  49.     }
  50.    
  51.     public sealed class TicTacPulse
  52.     {
  53.         public static TicTac ticTac;
  54.        
  55.         public static void IniciarReloj()
  56.         {
  57.             if (Thread.CurrentThread.Name.Equals ("Tic"))
  58.             {
  59.                 // Repite el sonido tic 5 veces:
  60.                 for (int i = 1; i <= 5; ++i)
  61.                 {
  62.                     ticTac.Tic(true);
  63.                 }
  64.                
  65.                 // Detiene el sonido tic:
  66.                 ticTac.Tic (false);
  67.             }
  68.             else
  69.             {
  70.                 // Repite el sonido tac 5 veces:
  71.                 for (int i = 1; i <= 5; ++i)
  72.                 {
  73.                     ticTac.Tac (true);
  74.                 }
  75.                
  76.                 // Detiene el sonido tac:
  77.                 ticTac.Tac (false);
  78.             }
  79.         }
  80.        
  81.         public static void Main()
  82.         {
  83.             ticTac = new TicTac();
  84.            
  85.             // Crea dos threads:
  86.             Thread t1 = new Thread (IniciarReloj);
  87.             t1.Name = "Tic";
  88.             Thread t2 = new Thread (IniciarReloj);
  89.             t2.Name = "Tac";
  90.            
  91.             t1.Start ();
  92.             t2.Start ();
  93.            
  94.             t1.Join();
  95.             t2.Join();
  96.            
  97.             Console.WriteLine ("\nEl reloj se ha detenido. Presione Enter.");
  98.             Console.ReadLine ();
  99.         }
  100.     }
  101. }