Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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());
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement