Skip to content

Instantly share code, notes, and snippets.

@Fhernd
Created June 18, 2014 18:01
Show Gist options
  • Save Fhernd/47003042f937f4548451 to your computer and use it in GitHub Desktop.
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#.
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