易采站长站为您分析C#多线程处理多个队列数据的方法,涉及C#线程与队列的相关操作技巧,需要的朋友可以参考下
本文实例讲述了C#多线程处理多个队列数据的方法。。具体实现方法如下:
- using System; using System.Collections.Generic;
- using System.Linq; using System.Text;
- using System.Threading; using System.Collections;
- using System.Windows.Forms; namespace ThredProcessQueue
- { //用于顯示狀態的代理方法類型定義
- public delegate void DelegateShowStateInfo(string state); /// <summary>
- /// 測試器 /// </summary>
- public class QueueTester {
- private static bool _Exit = false; //標記是否已中斷測試程序 private static Form _OwnerForm; //測試的窗體
- private static DelegateShowStateInfo _StateMethod; private static IList _Queue1 = new ArrayList(); //Queue1的數據
- private static IList _Queue2 = new ArrayList(); //Queue2的數據 private static IList _Queue3 = new ArrayList(); //Queue3的數據
- public static void StopThread()
- { _Exit = true;
- _OwnerForm = null; }
- public static void Testing(Form sender, DelegateShowStateInfo method) {
- _StateMethod = method; _OwnerForm = sender;
- _Exit = false; ThreadPool.QueueUserWorkItem(MainTestThread);
- ThreadPool.QueueUserWorkItem(Queue1Thread); //啟動Queue1線程 ThreadPool.QueueUserWorkItem(Queue2Thread); //啟動Queue2線程
- } //測試用的主線程,循環向隊列1中壓入數據。
- public static void MainTestThread(object state) {
- Random R = new Random(1); double V = 0;
- while (_Exit == false) {
- //在while(true)里一直对数据进行读取,然后放到queue1中, //与此同时如果queue1中有数据,则线程1就开启










