using System;
using System.Threading;
namespace Recetas.Multithreading.Cap01
{
public sealed class UsoDelegadoThreadStart
{
private int valor;
public UsoDelegadoThreadStart (int v)
{
valor = v;
}
// Método que será ejecutado en un nuevo thread:
public void MetodoThread()
{
Console.WriteLine ("El valor de la variable `valor` es: {0}", valor.ToString());
}
public static void Main()
{
// Creamos una instancia de `UsoDelegadoThreadStart` e inicializamos en 7
// el campo `valor`:
UsoDelegadoThreadStart nt = new UsoDelegadoThreadStart(7);
// Creamos un nuevo `Thread` y pasamos como argumento al constructor
// una instancia del delegado `ThreadStart`, el cual encapsula
// al método de instancia `MetodoThread`:
Thread thread = new Thread( new ThreadStart(nt.MetodoThread));
// Se inicia la ejecución del thread:
thread.Start();
Console.WriteLine( "El método `Main` ha terminado. Presione Enter para terminar...");
Console.ReadLine ();
}
}
}
dXNpbmcgU3lzdGVtOwp1c2luZyBTeXN0ZW0uVGhyZWFkaW5nOwogCm5hbWVzcGFjZSBSZWNldGFzLk11bHRpdGhyZWFkaW5nLkNhcDAxCnsKICAgIHB1YmxpYyBzZWFsZWQgY2xhc3MgVXNvRGVsZWdhZG9UaHJlYWRTdGFydAogICAgewogICAgICAgIHByaXZhdGUgaW50IHZhbG9yOwogICAgICAgICAKICAgICAgICBwdWJsaWMgVXNvRGVsZWdhZG9UaHJlYWRTdGFydCAoaW50IHYpCiAgICAgICAgewogICAgICAgICAgICB2YWxvciA9IHY7CiAgICAgICAgfQogICAgICAgICAKICAgICAgICAvLyBNw6l0b2RvIHF1ZSBzZXLDoSBlamVjdXRhZG8gZW4gdW4gbnVldm8gdGhyZWFkOgogICAgICAgIHB1YmxpYyB2b2lkIE1ldG9kb1RocmVhZCgpCiAgICAgICAgewogICAgICAgICAgICBDb25zb2xlLldyaXRlTGluZSAoIkVsIHZhbG9yIGRlIGxhIHZhcmlhYmxlIGB2YWxvcmAgZXM6IHswfSIsIHZhbG9yLlRvU3RyaW5nKCkpOwogICAgICAgIH0KICAgICAgICAgCiAgICAgICAgcHVibGlj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