Created
June 18, 2014 18:01
-
-
Save Fhernd/47003042f937f4548451 to your computer and use it in GitHub Desktop.
Demostración de la creación un nuevo thread con la clase Thread y el delegado ThreadStart. En C#.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Threading; | |
namespace Recetas.Multithreading.Cap01 | |
{ | |
public sealed class NuevoThread | |
{ | |
private int valor; | |
public NuevoThread (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 `NuevoThread` e inicializamos en 7 | |
// el campo `valor`: | |
NuevoThread nt = new NuevoThread(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 (); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment