C#多线程处理多个队列数据的方法

2019-12-26 12:29:58于海丽

易采站长站为您分析C#多线程处理多个队列数据的方法,涉及C#线程与队列的相关操作技巧,需要的朋友可以参考下

本文实例讲述了C#多线程处理多个队列数据的方法。。具体实现方法如下:

 

 
  1. using System;  using System.Collections.Generic; 
  2. using System.Linq;  using System.Text; 
  3. using System.Threading;  using System.Collections; 
  4. using System.Windows.Forms;  namespace ThredProcessQueue 
  5. {  //用于顯示狀態的代理方法類型定義  
  6. public delegate void DelegateShowStateInfo(string state);  /// <summary>  
  7. /// 測試器   /// </summary>  
  8. public class QueueTester  { 
  9. private static bool _Exit = false; //標記是否已中斷測試程序   private static Form _OwnerForm; //測試的窗體  
  10. private static DelegateShowStateInfo _StateMethod;  private static IList _Queue1 = new ArrayList(); //Queue1的數據  
  11. private static IList _Queue2 = new ArrayList(); //Queue2的數據   private static IList _Queue3 = new ArrayList(); //Queue3的數據  
  12.   public static void StopThread() 
  13. {  _Exit = true; 
  14. _OwnerForm = null;  } 
  15. public static void Testing(Form sender, DelegateShowStateInfo method)  { 
  16. _StateMethod = method;  _OwnerForm = sender; 
  17. _Exit = false;  ThreadPool.QueueUserWorkItem(MainTestThread); 
  18. ThreadPool.QueueUserWorkItem(Queue1Thread); //啟動Queue1線程   ThreadPool.QueueUserWorkItem(Queue2Thread); //啟動Queue2線程  
  19. }  //測試用的主線程,循環向隊列1中壓入數據。  
  20. public static void MainTestThread(object state)  { 
  21. Random R = new Random(1);  double V = 0; 
  22. while (_Exit == false)  { 
  23. //在while(true)里一直对数据进行读取,然后放到queue1中,   //与此同时如果queue1中有数据,则线程1就开启