Created
July 5, 2014 16:28
-
-
Save Fhernd/8c7981b1fee71a30f5a8 to your computer and use it in GitHub Desktop.
Demostración del uso de la propiedad System.Threading.Thread.ThreadState 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 | |
{ | |
public sealed class EstadosThread | |
{ | |
public static void Main() | |
{ | |
Console.WriteLine ("\nInicio de la aplicación..."); | |
// Creación de threads: | |
Thread t1 = new Thread (new ThreadStart (ImprimirNumerosConEstado)); | |
Thread t2 = new Thread (new ThreadStart (Tarea)); | |
Console.WriteLine ("\nEstado actual del thread `t1`: {0}", t1.ThreadState.ToString()); | |
// Inicialización de los threads: | |
t1.Start(); | |
t2.Start(); | |
for (int i = 1; i < 30; ++i) | |
{ | |
Console.WriteLine ("Estado actual del thread `t1`: {0}", t1.ThreadState.ToString()); | |
} | |
// Pausa por 6 segundos del thread Main: | |
Thread.Sleep (TimeSpan.FromSeconds (6)); | |
// Aborta el thread `t1`: | |
t1.Abort(); | |
// Presentación de resultados: | |
Console.WriteLine ("\nUn thread ha sido abortado."); | |
Console.WriteLine ("Estado del thread `t1`: {0}", t1.ThreadState.ToString()); | |
Console.WriteLine ("Estado del thread `t2`: {0}\n", t2.ThreadState.ToString()); | |
} | |
private static void Tarea() | |
{ | |
Thread.Sleep (TimeSpan.FromSeconds (2)); | |
} | |
private static void ImprimirNumerosConEstado() | |
{ | |
Console.WriteLine ("\nInicio del método `ImprimirNumerosConEstado`..."); | |
Console.WriteLine ("\nEstado actual thread actual: {0}", Thread.CurrentThread.ThreadState.ToString()); | |
for (int i = 1; i < 10; ++i) | |
{ | |
Thread.Sleep (TimeSpan.FromSeconds (2)); | |
Console.WriteLine ("Valor de `i`: {0}", i.ToString()); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment