C#多線程處理多個(gè)隊(duì)列數(shù)據(jù)的方法
本文實(shí)例講述了C#多線程處理多個(gè)隊(duì)列數(shù)據(jù)的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Collections; using System.Windows.Forms; namespace ThredProcessQueue { //用于顯示狀態(tài)的代理方法類型定義 public delegate void DelegateShowStateInfo(string state); /// <summary> /// 測(cè)試器 /// </summary> public class QueueTester { private static bool _Exit = false; //標(biāo)記是否已中斷測(cè)試程序 private static Form _OwnerForm; //測(cè)試的窗體 private static DelegateShowStateInfo _StateMethod; private static IList _Queue1 = new ArrayList(); //Queue1的數(shù)據(jù) private static IList _Queue2 = new ArrayList(); //Queue2的數(shù)據(jù) private static IList _Queue3 = new ArrayList(); //Queue3的數(shù)據(jù) 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); //啟動(dòng)Queue1線程 ThreadPool.QueueUserWorkItem(Queue2Thread); //啟動(dòng)Queue2線程 } //測(cè)試用的主線程,循環(huán)向隊(duì)列1中壓入數(shù)據(jù)。 public static void MainTestThread(object state) { Random R = new Random(1); double V = 0; while (_Exit == false) { //在while(true)里一直對(duì)數(shù)據(jù)進(jìn)行讀取,然后放到queue1中, //與此同時(shí)如果queue1中有數(shù)據(jù),則線程1就開(kāi)啟 //臨時(shí)數(shù)據(jù),隨機(jī)數(shù) V = R.NextDouble(); _Queue1.Add(V); //把數(shù)據(jù)插入到隊(duì)列1 Application.DoEvents(); ShowState(); Thread.Sleep(100);//生成隨機(jī)數(shù)太快,為了看清效果,暫停n毫秒 } } //對(duì)queue1中的數(shù)據(jù)進(jìn)行處理,處理后放到queue2中 public static void Queue1Thread(object state) { while (_Exit == false) { while (_Queue1.Count > 0) { //對(duì)queue1中的數(shù)據(jù)進(jìn)行處理,處理后放到queue2中 _Queue2.Add(_Queue1[0]); _Queue1.RemoveAt(0); Application.DoEvents(); ShowState(); } } } //對(duì)queue2中的數(shù)據(jù)進(jìn)行處理,處理后放到queue3中 public static void Queue2Thread(object state) { while (_Exit == false) { while (_Queue2.Count > 0) { //對(duì)queue1中的數(shù)據(jù)進(jìn)行處理,處理后放到queue2中 _Queue3.Add(_Queue2[0]); _Queue2.RemoveAt(0); Application.DoEvents(); ShowState(); } } } //用于監(jiān)視各隊(duì)列狀態(tài)的線程 public static void ShowState() { string stateInfo = QueueTester._Queue1.Count.ToString() " -> " QueueTester._Queue2.Count.ToString() " -> " QueueTester._Queue3.Count.ToString(); try { if (_OwnerForm != null) { _OwnerForm.Invoke(_StateMethod, stateInfo); Application.DoEvents(); } } catch { } } } }
希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。
相關(guān)文章
使用策略模式實(shí)現(xiàn)報(bào)警服務(wù)示例詳解(短信報(bào)警)
服務(wù)的功能:這個(gè)服務(wù)就是能夠?qū)崿F(xiàn)多通路報(bào)警的服務(wù),比如郵件報(bào)警、客戶端報(bào)警、短信報(bào)警等,該服務(wù)靈活性還不錯(cuò),比較方便擴(kuò)展2014-01-01Unity UGUI的HorizontalLayoutGroup水平布局組件介紹使用
這篇文章主要為大家介紹了Unity UGUI的HorizontalLayoutGroup水平布局組件介紹使用,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07C#如何通過(guò)probing指定dll尋找文件夾詳解
這篇文章主要給大家介紹了關(guān)于C#如何通過(guò)probing指定dll尋找文件夾的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-12-12C#區(qū)分中英文按照指定長(zhǎng)度截取字符串的方法
這篇文章主要介紹了C#區(qū)分中英文按照指定長(zhǎng)度截取字符串的方法,涉及C#操作字符串的正則匹配與截取等常用操作技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-03-03幾分鐘搞懂c#之FileStream對(duì)象讀寫(xiě)大文件(推薦)
這篇文章主要介紹了c#之FileStream對(duì)象讀寫(xiě)大文件,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04C#實(shí)現(xiàn)根據(jù)給出的相對(duì)地址獲取網(wǎng)站絕對(duì)地址的方法
這篇文章主要介紹了C#實(shí)現(xiàn)根據(jù)給出的相對(duì)地址獲取網(wǎng)站絕對(duì)地址的方法,涉及C#URL及字符串操作的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03使用Unity3D實(shí)現(xiàn)選中物體消融特效的方法詳解
消融特效中基Shader?Graph實(shí)現(xiàn)了消融特效,本文將基于?Shader?實(shí)現(xiàn)消融特效,當(dāng)前實(shí)現(xiàn)消融特效的方法主要有?Alpha?測(cè)試消融、clip(或?discard)消融,它們的本質(zhì)都是隨機(jī)丟棄一些片元,以實(shí)現(xiàn)消融效果,文中有詳細(xì)代碼示例,需要的朋友可以參考下2023-10-10