Advertisement
Fhernd

UsoPropiedadThreadState.cs

Jul 5th, 2014
1,194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.80 KB | None | 0 0
  1. using System;
  2. using System.Threading;
  3.  
  4. namespace Recetas.Multithreading.Cap01
  5. {
  6.     public sealed class UsoPropiedadThreadState
  7.     {
  8.         public static void Main()
  9.         {
  10.             Thread threadNuevo = new Thread (new ThreadStart (ImprimirNumerosConRetraso));
  11.            
  12.             // Antes de empezar la ejecución del thread, este es su estado:
  13.             Console.WriteLine ("\nEstado antes de empezar la ejecución el thread: `{0}`.", threadNuevo.ThreadState.ToString());
  14.            
  15.             // Inicio de la ejecución del thread:
  16.             threadNuevo.Start();
  17.            
  18.             // Aborta el thread:
  19.             threadNuevo.Abort();
  20.            
  21.             // Nuevo estado del thread:
  22.             Console.WriteLine ("\nNuevo estado del thread: `{0}`.", threadNuevo.ThreadState.ToString());
  23.            
  24.             // Vuelve a iniciar el thread:
  25.             threadNuevo = new Thread (ImprimirNumerosConRetraso);
  26.             threadNuevo.Start();
  27.            
  28.             // Espera a que la ejecución del thread `threadNuevo` finalice:
  29.             threadNuevo.Join();
  30.            
  31.             // Después de finalizar la ejecución del thread, este es su nuevo estado:
  32.             Console.WriteLine ("\nEstado del thread `nuevoThread` después de finalizar su ejecución: `{0}`.", threadNuevo.ThreadState.ToString());
  33.         }
  34.        
  35.         private static void ImprimirNumerosConRetraso()
  36.         {
  37.             Console.WriteLine( "\nEstado actual del thread `nuevoThread`: `{0}`.\n", Thread.CurrentThread.ThreadState.ToString());
  38.            
  39.             for (int i = 0; i < 9; ++i)
  40.             {
  41.                 Thread.Sleep (1000);
  42.                
  43.                 Console.WriteLine ("Valor actual de `i`: {0}", i.ToString());
  44.             }
  45.         }
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement