Skip to content

Instantly share code, notes, and snippets.

@Fhernd
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Fhernd/17c4d2620764f64e7c74 to your computer and use it in GitHub Desktop.
Save Fhernd/17c4d2620764f64e7c74 to your computer and use it in GitHub Desktop.
Uso de la versión sobrecargada de Join con cero parámetros. En C#.
using System;
using System.Threading;
namespace Recetas.Multithreading.Cap01
{
internal sealed class UsoThreadJoin
{
// Declaración de threads:
private static Thread threadMain, thread1, thread2;
public static void Main()
{
threadMain = Thread.CurrentThread;
thread1 = new Thread (ProcesoThread);
// El nombre facilita su identificación:
thread1.Name = "Thread1";
thread1.Start();
// Crea una instancia de Thread:
thread2 = new Thread (ProcesoThread);
// El nombre facilita su identificación:
thread2.Name = "Thread2";
thread2.Start();
}
// Método compatible con la firma del delegado
// ThreadStart:
private static void ProcesoThread()
{
Console.WriteLine ("\nThread en curso: {0}", Thread.CurrentThread.Name);
if (Thread.CurrentThread.Name.Equals ("Thread1") &&
thread2.ThreadState != ThreadState.Unstarted)
{
// Pone en espera el thread actual hasta
// que el thread sobre el que se invoca Join finaliza:
thread2.Join ();
}
// Pausa el thread por 4 segundos:
Thread.Sleep (4000);
Console.WriteLine ("\nThread en curso: {0}", Thread.CurrentThread.Name);
Console.WriteLine ("`thread1`: {0}", thread1.ThreadState);
Console.WriteLine ("`thread2`: {0}\n", thread2.ThreadState);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment