Advertisement
Fhernd

UsoDelegadoParameterizedThreadStart.cs

Jul 24th, 2014
2,209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.79 KB | None | 0 0
  1. using System;
  2. using System.Threading;
  3.  
  4. namespace Recetas.Multithreading.Cap01.R0108
  5. {
  6.     public sealed class UsoDelegadoParameterizedThreadStart
  7.     {
  8.         public static void Main()
  9.         {
  10.             // Creación de una nueva instancia de `Thread`.
  11.             // Al constructor le pasamos una instancia del delegado
  12.             // ParameterizedThreadStart. Este último encapsula al
  13.             // método Tarea1:
  14.             Thread threadNuevo = new Thread (
  15.                 new ParameterizedThreadStart (Tarea1)
  16.             );
  17.            
  18.             // Aquí utilizamos la versión sobrecargad del método
  19.             // Start que recibe como argumento una instancia de
  20.             // `Object`:
  21.             threadNuevo.Start (7);
  22.            
  23.             // Creación de una instancia de UsoDelegadoParameterizedThreadStart:
  24.             UsoDelegadoParameterizedThreadStart obj = new
  25.                 UsoDelegadoParameterizedThreadStart();
  26.                
  27.             // Creamos otro thread:
  28.             threadNuevo = new Thread (obj.Tarea2);
  29.            
  30.             // invocamos de nuevo al método Start sobre
  31.             // el nuevo thread:
  32.             threadNuevo.Start ("Blog xCSw");
  33.         }
  34.    
  35.         // Método estático:
  36.         public static void Tarea1(object datos)
  37.         {
  38.             Console.WriteLine ("\nInicio ejecución método estático sobre un nuevo thread.");
  39.             Console.WriteLine ("Dato pasado como argumento: `{0}`.", datos);
  40.         }
  41.        
  42.         // Método de instancia:
  43.         public void Tarea2(object datos)
  44.         {
  45.             Console.WriteLine ("\nInicio ejecución método de instancia sobre un nuevo thread.");
  46.             Console.WriteLine ("Dato pasado como argumento: `{0}`.", datos);
  47.         }
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement