Created
August 13, 2014 23:56
-
-
Save Fhernd/a1530f1984dad523bdba to your computer and use it in GitHub Desktop.
Uso de la propiedad Thread.IsAlive para determinar cuándo un thread ha finalizado su ejecución.
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.CSharp.Cap04.R0412 | |
{ | |
public sealed class UsoIsAlive | |
{ | |
public static void Main() | |
{ | |
// Creación Thread: | |
Thread t = new Thread(Tarea); | |
// Inicio de la ejecución: | |
t.Start(); | |
// Ciclo while para comprobar el estado del thread. | |
// Determina cuándo ha finalizado y continua con la | |
// siguiente tarea: | |
while (t.IsAlive){} // Uso ineficiente de ciclos de procesador | |
Console.WriteLine ("\nPresione Enter para finalizar.\n"); | |
Console.ReadLine (); | |
} | |
// Ejecución sobre un nuevo thread: | |
private static void Tarea() | |
{ | |
for (int i = 1; i <= 5; ++i) | |
{ | |
Thread.Sleep(1000); | |
Console.WriteLine ("Un segundo ha transcurrido..."); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment