Advertisement
Fhernd

UsoManualResetEvent.cs

Jul 16th, 2014
1,596
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.48 KB | None | 0 0
  1. using System;
  2. using System.Threading;
  3.  
  4. namespace Recetas.CSharp.Cap04.R0408
  5. {
  6.     public sealed class UsoManualResetEvent
  7.     {
  8.         // Con una instancia de ManualResetEvent podemos
  9.         // bloquear o liberar threads de forma manual:
  10.         private static ManualResetEvent mre = new ManualResetEvent(false);
  11.        
  12.         public static void Main()
  13.         {
  14.             Console.WriteLine ("\nInicia la ejecución de 3 threads:");
  15.            
  16.             for (int i = 0; i <= 2; ++i)
  17.             {
  18.                 Thread t = new Thread (Proceso);
  19.                 t.Name = String.Format ("Thread No. {0}", i.ToString());
  20.                 t.Start();
  21.             }
  22.            
  23.             Thread.Sleep (500);
  24.             Console.WriteLine ("\nUna vez que todos los threadas se han iniciado, " +
  25.                                 "presione Enter para invocar al método `Set`" +
  26.                                 " para liberar todos los threads.\n");
  27.             Console.ReadLine ();
  28.            
  29.             mre.Set();
  30.            
  31.             Thread.Sleep (500);
  32.         }
  33.        
  34.         private static void Proceso()
  35.         {
  36.             string nombreThread = Thread.CurrentThread.Name;
  37.            
  38.             Console.WriteLine ("{0} ha iniciado su ejecución e invocado el método WaitOne().", nombreThread);
  39.            
  40.             mre.WaitOne ();
  41.            
  42.             Console.WriteLine ("El thread {0} ha finalizado.", nombreThread);
  43.         }
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement