Advertisement
Fhernd

CrearNuevoThread.cs

Sep 18th, 2014
827
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.23 KB | None | 0 0
  1. using System;
  2. using System.Threading;
  3.  
  4. namespace Recetas.Multithreading.Cap01
  5. {
  6.     public sealed class NuevoThread
  7.     {
  8.         private int valor;
  9.        
  10.         public NuevoThread (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 `NuevoThread` e inicializamos en 7
  24.             // el campo `valor`:
  25.             NuevoThread nt = new NuevoThread(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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement