Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. // OrtizOL - xCSw - http://ortizol.blogspot.com
  2.  
  3. using System;
  4. using System.Threading;
  5.  
  6. namespace Receta.Multithreading.R0204
  7. {
  8.     public class EnvioNotificacionesThreads
  9.     {
  10.         private static AutoResetEvent eventoSecundario = new AutoResetEvent(false);
  11.         private static AutoResetEvent eventoPrincipal = new AutoResetEvent(false);
  12.        
  13.         public static void Main()
  14.         {
  15.             Console.WriteLine();
  16.            
  17.             Thread nuevoThread = new Thread( () => Proceso(10));
  18.             nuevoThread.Start();
  19.            
  20.             Console.WriteLine ("Thread `Main`: A espera que el nuevo thread finalice su primera tarea.");
  21.             // Señal de bloqueo del thread `nuevoThread`:
  22.             eventoSecundario.WaitOne();
  23.            
  24.             Console.WriteLine("Thread `Main`: La primera de `Proceso` tarea ha finalizado satisfactoriamente.");
  25.            
  26.             Console.WriteLine("Thread `Main`: Ejecutando una tarea sobre `Main`.");
  27.             Thread.Sleep(TimeSpan.FromSeconds(5));
  28.             // Señal de desbloqueo del thread `Main`:
  29.             eventoPrincipal.Set();
  30.            
  31.             Console.WriteLine("Thread `Main`: En ejecución segunda tarea de `Proceso` sobre el nuevo thread.");
  32.             // Señal de bloqueo del thread `nuevoThread`:
  33.             eventoSecundario.WaitOne();
  34.            
  35.             Console.WriteLine("Thread `Main`: La segunda operación de `Proceso` ha finalizado.");
  36.            
  37.             Console.WriteLine();
  38.         }
  39.        
  40.         public static void Proceso(int segundosEspera)
  41.         {
  42.             Console.WriteLine("Thread `Proceso`: Inicio de proceso largo...");
  43.             Thread.Sleep(TimeSpan.FromSeconds(segundosEspera));
  44.             Console.WriteLine("Thread `Proceso`: Primera tarea de `Proceo` finalizada.");
  45.            
  46.             // Señal de desbloqueo del thread `nuevoThread`:
  47.             eventoSecundario.Set();
  48.            
  49.             Console.WriteLine("Thread `Proceso`: A espera de que el thread `Main` finalice.");
  50.            
  51.             // Señal de bloqueo del thread `Main`:
  52.             eventoPrincipal.WaitOne();
  53.            
  54.             Console.WriteLine("Thread `Proceso`: Inicio segunda operación del proceso...");
  55.             Thread.Sleep(TimeSpan.FromSeconds(segundosEspera));
  56.            
  57.             Console.WriteLine("Thread `Proceso`: ¡Proceo finalizado!");
  58.            
  59.             // Señal de desbloqueo thread `nuevoThread`:
  60.             eventoSecundario.Set();
  61.         }
  62.     }
  63. }