using System;
using System.Threading ;
namespace Recetas.CSharp .Cap04 .R0408
{
public sealed class UsoManualResetEvent
{
// Con una instancia de ManualResetEvent podemos
// bloquear o liberar threads de forma manual:
private static ManualResetEvent mre = new ManualResetEvent( false ) ;
public static void Main( )
{
Console.WriteLine ( "\n Inicia la ejecución de 3 threads:" ) ;
for ( int i = 0 ; i <= 2 ; ++ i)
{
Thread t = new Thread ( Proceso) ;
t.Name = String.Format ( "Thread No. {0}" , i.ToString ( ) ) ;
t.Start ( ) ;
}
Thread.Sleep ( 500 ) ;
Console.WriteLine ( "\n Una vez que todos los threadas se han iniciado, " +
"presione Enter para invocar al método `Set`" +
" para liberar todos los threads.\n " ) ;
Console.ReadLine ( ) ;
mre.Set ( ) ;
Thread.Sleep ( 500 ) ;
}
private static void Proceso( )
{
string nombreThread = Thread.CurrentThread .Name ;
Console.WriteLine ( "{0} ha iniciado su ejecución e invocado el método WaitOne()." , nombreThread) ;
mre.WaitOne ( ) ;
Console.WriteLine ( "El thread {0} ha finalizado." , nombreThread) ;
}
}
}
dXNpbmcgU3lzdGVtOwp1c2luZyBTeXN0ZW0uVGhyZWFkaW5nOwoKbmFtZXNwYWNlIFJlY2V0YXMuQ1NoYXJwLkNhcDA0LlIwNDA4CnsKICAgIHB1YmxpYyBzZWFsZWQgY2xhc3MgVXNvTWFudWFsUmVzZXRFdmVudAogICAgewogICAgICAgIC8vIENvbiB1bmEgaW5zdGFuY2lhIGRlIE1hbnVhbFJlc2V0RXZlbnQgcG9kZW1vcyAKICAgICAgICAvLyBibG9xdWVhciBvIGxpYmVyYXIgdGhyZWFkcyBkZSBmb3JtYSBtYW51YWw6CiAgICAgICAgcHJpdmF0ZSBzdGF0aWMgTWFudWFsUmVzZXRFdmVudCBtcmUgPSBuZXcgTWFudWFsUmVzZXRFdmVudChmYWxzZSk7CiAgICAgICAgCiAgICAgICAgcHVibGljIHN0YXRpYyB2b2lkIE1haW4oKQogICAgICAgIHsKICAgICAgICAgICAgQ29uc29sZS5Xcml0ZUxpbmUgKCJcbkluaWNpYSBsYSBlamVjdWNpw7NuIGRlIDMgdGhyZWFkczoiKTsKICAgICAgICAgICAgCiAgICAgICAgICAgIGZvciAoaW50IGkgPSAwOyBpIDw9IDI7ICsraSkKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgVGhyZWFkIHQgPSBuZXcgVGhyZWFkIChQcm9jZXNvKTsKICAgICAgICAgICAgICAgIHQuTmFtZSA9IFN0cmluZy5Gb3JtYXQgKCJUaHJlYWQgTm8uIHswfSIsIGkuVG9TdHJpbmcoKSk7CiAgICAgICAgICAgICAgICB0LlN0YXJ0KCk7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgCiAgICAgICAgICAgIFRocmVhZC5TbGVlcCAoNTAwKTsKICAgICAgICAgICAgQ29uc29sZS5Xcml0ZUxpbmUgKCJcblVuYSB2ZXogcXVlIHRvZG9zIGxvcyB0aHJlYWRhcyBzZSBoYW4gaW5pY2lhZG8sICIgKyAKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAicHJlc2lvbmUgRW50ZXIgcGFyYSBpbnZvY2FyIGFsIG3DqXRvZG8gYFNldGAiICsgCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIiBwYXJhIGxpYmVyYXIgdG9kb3MgbG9zIHRocmVhZHMuXG4iKTsKICAgICAgICAgICAgQ29uc29sZS5SZWFkTGluZSAoKTsKICAgICAgICAgICAgCiAgICAgICAgICAgIG1yZS5TZXQoKTsKICAgICAgICAgICAgCiAgICAgICAgICAgIFRocmVhZC5TbGVlcCAoNTAwKTsKICAgICAgICB9CiAgICAgICAgCiAgICAgICAgcHJpdmF0ZSBzdGF0aWMgdm9pZCBQcm9jZXNvKCkKICAgICAgICB7CiAgICAgICAgICAgIHN0cmluZyBub21icmVUaHJlYWQgPSBUaHJlYWQuQ3VycmVudFRocmVhZC5OYW1lOwogICAgICAgICAgICAKICAgICAgICAgICAgQ29uc29sZS5Xcml0ZUxpbmUgKCJ7MH0gaGEgaW5pY2lhZG8gc3UgZWplY3VjacOzbiBlIGludm9jYWRvIGVsIG3DqXRvZG8gV2FpdE9uZSgpLiIsIG5vbWJyZVRocmVhZCk7CiAgICAgICAgICAgIAogICAgICAgICAgICBtcmUuV2FpdE9uZSAoKTsKICAgICAgICAgICAgCiAgICAgICAgICAgIENvbnNvbGUuV3JpdGVMaW5lICgiRWwgdGhyZWFkIHswfSBoYSBmaW5hbGl6YWRvLiIsIG5vbWJyZVRocmVhZCk7CiAgICAgICAgfQogICAgfQp9