Advertisement
Fhernd

InvocacionDelegadoEnPoolThreads.cs

Jul 19th, 2015
1,055
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.58 KB | None | 0 0
  1. // OrtizOL - xCSw - http://ortizol.blogspot.com
  2.  
  3. using System;
  4. using System.Threading;
  5.  
  6. namespace Receta.Multithreading.R0301
  7. {
  8.     public class InvocacionDelegadoEnPoolThreads
  9.     {
  10.         // Declaración de delegado:
  11.         private delegate string EjecutarEnPoolThread(out int idThread);
  12.        
  13.         public static void Main()
  14.         {
  15.             Console.WriteLine(Environment.NewLine);
  16.            
  17.             int idThread = 0;
  18.            
  19.             // Creación de thread (forma tradicional):
  20.             Thread t = new Thread(() => Proceso(out idThread));
  21.             t.Start();
  22.             t.Join();
  23.            
  24.             Console.WriteLine("ID Thread: {0}\n", idThread);
  25.            
  26.             // Invocación de delegado (método basado en pool de threads):
  27.             EjecutarEnPoolThread delegadoEnPool = Proceso;
  28.            
  29.             IAsyncResult r = delegadoEnPool.BeginInvoke(out idThread,
  30.                              Callback, "Invocación asincrónica de delegado");
  31.            
  32.             // Obtiene el resultado de la ejecución de `Proceso`:
  33.             string resultado = delegadoEnPool.EndInvoke(out idThread, r);
  34.            
  35.             Console.WriteLine("ID de thread del pool de threads: {0}",
  36.                               idThread.ToString());
  37.                              
  38.             Console.WriteLine(resultado);
  39.            
  40.             Thread.Sleep(TimeSpan.FromSeconds(2));
  41.            
  42.             Console.WriteLine(Environment.NewLine);
  43.         }
  44.        
  45.         private static void Callback(IAsyncResult ar)
  46.         {
  47.             Console.WriteLine("Inicio de callback...");
  48.             Console.WriteLine("Estado de callback: {0}", ar.AsyncState);
  49.             Console.WriteLine("¿Thread en pool de threads?: {0}",
  50.                               Thread.CurrentThread.IsThreadPoolThread);
  51.             Console.WriteLine("ID de thread del pool de threads: {0}",
  52.                               Thread.CurrentThread.ManagedThreadId);
  53.         }
  54.        
  55.         private static String Proceso(out int idThread)
  56.         {
  57.             Console.WriteLine("Iniciando ejecución de `Proceso`...");
  58.             Console.WriteLine("¿Thread en pool de threads?: {0}",
  59.                               Thread.CurrentThread.IsThreadPoolThread);
  60.             Thread.Sleep(TimeSpan.FromSeconds(2));
  61.            
  62.             idThread = Thread.CurrentThread.ManagedThreadId;
  63.            
  64.             return String.Format("ID de thread asignado desde pool de threads: {0}",
  65.                                   idThread);
  66.         }
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement