当前位置:首页>网络学院>程序开发>c#教程>文章内容

ThreadPoolHelper线程池源代码

[ 来源:http://www.it55.com | 作者: | 时间:2007-12-24 | 收藏 | 推荐 ] 【


  
  最后,我们需要对这个ThreadPoolHelper.QueueUserWorkItem进行改进,使其支持params,也只有这样才能实现类似上面二分算法的外部表现。
  
  增加以下代码:
  
  public static bool QueueUserWorkItems(params WaitCallbackNew[] proc)
  {
  bool result = true;
  foreach (WaitCallbackNew tp in proc)
  {
  result &= QueueUserWorkItem(tp);
  }
  return result;
  }
  如此就可以实现形如:
  
  ThreadPoolHelper.QueueUserWorkItems(ThreadProc, ThreadProc, ThreadProc);
  再加上.NET2.0以上版本对匿名委托的支持,就可以有:
  
  ThreadPoolHelper.QueueUserWorkItems(
  delegate { Console.WriteLine("a"); },
  delegate { Console.WriteLine("b"); },
  delegate { Console.WriteLine("c"); }
  );
  这样的代码了。
  
  
  全部示例:
  
  //ThreadPoolHelper.cs
  
  
  ThreadPoolHelper.cs
  using System;
  using System.Threading;
  
  namespace CA_ThreadPool
  {
   /// <summary>
   /// Sample helper class for ThreadPool.
   /// </summary>
   class ThreadPoolHelper
   {
   /// <summary>
   /// The delegate for explicit method.
   /// </summary>
   public delegate void WaitCallbackNew();
  
   /// <summary>
   /// The WaitCallback delegate object in actual of our thread pool.
   /// </summary>
   /// <param name="state"></param>
   static void Callback(object state)
   {
   (state as WaitCallbackNew)();
   }
  
   /// <summary>
   /// Queues a method for execution.
   /// The method executes when a thread pool thread becomes available.
   /// </summary>
   /// <param name="proc"></param>
   /// <returns></returns>
   public static bool QueueUserWorkItem(WaitCallbackNew callback)
   {
   return ThreadPool.QueueUserWorkItem(new WaitCallback(Callback), callback);
   }
  
   /// <summary>
   /// Queues a few method for execution.
   /// The method executes when a thread pool thread becomes available.
   /// </summary>
   /// <param name="proc"></param>
   /// <returns></returns>
   public static bool QueueUserWorkItems(params WaitCallbackNew[] proc)
   {
   bool result = true;
   foreach (WaitCallbackNew tp in proc)
   {
   result &= QueueUserWorkItem(tp);
   }
   return result;
   }
   }
  }
  
  
  
  
  //ThreadPoolHelperExample.cs
  
  
  ThreadPoolHelperExample.cs
  using System;
  using System.Threading;
  
  namespace CA_ThreadPool
  {
   public class Example
   {
   public static void Main()
   {
   // Queue the task.
   ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc));
   ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc));
   ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc));
   //
   //The code above will be predigested to the code down!
   //
   ThreadPoolHelper.QueueUserWorkItem(ThreadProc);
   ThreadPoolHelper.QueueUserWorkItem(ThreadProc);
   ThreadPoolHelper.QueueUserWorkItem(ThreadProc);
   //
   //The code above will be predigested to the code down!
   //
   ThreadPoolHelper.QueueUserWorkItems(ThreadProc, ThreadProc, ThreadProc);
   //
   //The code above will be evolved to the code down!
   //
   ThreadPoolHelper.QueueUserWorkItems(
   delegate { Console.WriteLine("a"); },

(编辑:IT资讯之家 www.it55.com

返回顶部
共3页: 上一页 [1] 2 [3] 下一页  

网友评论

[以下评论为网友观点,不代表本站。请自觉遵守互联网相关政策法规,所有连带责任均有评论者自负。]
[不超过250字]