Advertisement
Fhernd

UsoThreadJoin.cs

Jun 26th, 2014
1,669
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.66 KB | None | 0 0
  1. using System;
  2. using System.Threading;
  3.  
  4. namespace Recetas.Multithreading.Cap01
  5. {
  6.     internal sealed class UsoThreadJoin
  7.     {
  8.         // Declaración de threads:
  9.         private static Thread threadMain, thread1, thread2;    
  10.    
  11.         public static void Main()
  12.         {
  13.             threadMain = Thread.CurrentThread;
  14.            
  15.             thread1 = new Thread (ProcesoThread);
  16.             // El nombre facilita su identificación:
  17.             thread1.Name = "Thread1";
  18.             thread1.Start();
  19.            
  20.             // Crea una instancia de Thread:
  21.             thread2 = new Thread (ProcesoThread);
  22.             // El nombre facilita su identificación:
  23.             thread2.Name = "Thread2";
  24.             thread2.Start();
  25.         }
  26.        
  27.         // Método compatible con la firma del delegado
  28.         // ThreadStart:
  29.         private static void ProcesoThread()
  30.         {
  31.             Console.WriteLine ("\nThread en curso: {0}", Thread.CurrentThread.Name);
  32.            
  33.             if (Thread.CurrentThread.Name.Equals ("Thread1") &&
  34.                 thread2.ThreadState != ThreadState.Unstarted)
  35.             {
  36.                 // Pone en espera el thread actual hasta
  37.                 // que el thread sobre el que se invoca Join finaliza:
  38.                 thread2.Join ();
  39.             }
  40.            
  41.             // Pausa el thread por 4 segundos:
  42.             Thread.Sleep (4000);
  43.            
  44.             Console.WriteLine ("\nThread en curso: {0}", Thread.CurrentThread.Name);
  45.             Console.WriteLine ("`thread1`: {0}", thread1.ThreadState);
  46.             Console.WriteLine ("`thread2`: {0}\n", thread2.ThreadState);
  47.         }
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement