Created
June 26, 2014 22:39
-
-
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#.
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 | |
{ | |
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