Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. using System;
  2. using System.Threading;
  3.  
  4. namespace Recetas.Multithreading.Cap01
  5. {
  6.     internal sealed class UsoAbortObject
  7.     {
  8.         public static void Main()
  9.         {
  10.             Console.WriteLine();
  11.        
  12.             Thread threadNuevo = new Thread (EjecutarTarea);
  13.             threadNuevo.Start();
  14.            
  15.             Thread.Sleep (1000);
  16.            
  17.             // Aborto del thread `threadNuevo`:
  18.             Console.WriteLine ("Thread Main abortando el nuevo thread.");
  19.             threadNuevo.Abort( "Datos desde el thread Main.");
  20.            
  21.             // A espera a que el thread `threadNuevo` termine:
  22.             threadNuevo.Join ();
  23.            
  24.             Console.WriteLine ("\nEl thread `threadNuevo` ha finalizado.");
  25.             Console.WriteLine ("\nEl thread Main a punto de finalizar.\n");
  26.         }
  27.        
  28.         private static void EjecutarTarea ()
  29.         {
  30.             try
  31.             {
  32.                 while (true)
  33.                 {
  34.                     Console.WriteLine ("`threadNuevo` en ejecución...");
  35.                     Thread.Sleep (1000);
  36.                 }
  37.             }
  38.             catch(ThreadAbortException tae)
  39.             {
  40.                 Console.WriteLine ("\nMensaje excepción: {0}",
  41.                 (string)tae.ExceptionState);
  42.             }
  43.         }
  44.     }
  45. }