Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. using System;
  2. using System.Threading;
  3.  
  4. namespace Recetas.Multithreading.Cap01
  5. {
  6.     public sealed class UsoDelegadoThreadStart
  7.     {
  8.         private int valor;
  9.          
  10.         public UsoDelegadoThreadStart (int v)
  11.         {
  12.             valor = v;
  13.         }
  14.          
  15.         // Método que será ejecutado en un nuevo thread:
  16.         public void MetodoThread()
  17.         {
  18.             Console.WriteLine ("El valor de la variable `valor` es: {0}", valor.ToString());
  19.         }
  20.          
  21.         public static void Main()
  22.         {
  23.             // Creamos una instancia de `UsoDelegadoThreadStart` e inicializamos en 7
  24.             // el campo `valor`:
  25.             UsoDelegadoThreadStart nt = new UsoDelegadoThreadStart(7);
  26.              
  27.             // Creamos un nuevo `Thread` y pasamos como argumento al constructor
  28.             // una instancia del delegado `ThreadStart`, el cual encapsula
  29.             // al método de instancia `MetodoThread`:
  30.             Thread thread = new Thread( new ThreadStart(nt.MetodoThread));
  31.              
  32.             // Se inicia la ejecución del thread:
  33.             thread.Start();
  34.              
  35.             Console.WriteLine( "El método `Main` ha terminado. Presione Enter para terminar...");
  36.             Console.ReadLine ();
  37.         }
  38.     }
  39. }