Skip to content

Instantly share code, notes, and snippets.

@Fhernd
Created June 26, 2014 22:39
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/6dfed1e487cf07174dd9 to your computer and use it in GitHub Desktop.
Save Fhernd/6dfed1e487cf07174dd9 to your computer and use it in GitHub Desktop.
Uso de Join() para la espera de impresión de números en un nuevo thread. En C#.
using System;
using System.Threading;
namespace Recetas.Multithreading.Cap01
{
internal sealed class ImpresorNumeros
{
// Imprime los números del 0 al 9 introduciendo
// un retraso de 2 segundos por cada iteración
// del ciclo for:
private static void ImprimirNumerosConRetraso()
{
Console.WriteLine ("Inicio ejecución...");
for (int i = 0; i < 10; ++i)
{
// Retraso (pausa) de dos (2) segundos:
Thread.Sleep (TimeSpan.FromSeconds (2));
Console.WriteLine (i.ToString());
}
}
public static void Main()
{
Console.WriteLine ("\nIniciando la aplicación...");
Thread threadNuevo = new Thread (ImprimirNumerosConRetraso);
// Inicio del thread:
threadNuevo.Start();
// Ponemos en espera el thread Main hasta que
// el thread threadNuevo termine:
threadNuevo.Join();
Console.WriteLine ("\nThread Main terminó.\n");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment