Skip to content

Instantly share code, notes, and snippets.

@Fhernd
Created August 13, 2014 23:56
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/a1530f1984dad523bdba to your computer and use it in GitHub Desktop.
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.
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