C#多線程處理多個(gè)隊(duì)列數(shù)據(jù)的方法
更新時(shí)間:2015年07月03日 10:16:35 作者:程序猴
這篇文章主要介紹了C#多線程處理多個(gè)隊(duì)列數(shù)據(jù)的方法,涉及C#線程與隊(duì)列的相關(guān)操作技巧,需要的朋友可以參考下
本文實(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>
/// 測試器
/// </summary>
public class QueueTester
{
private static bool _Exit = false; //標(biāo)記是否已中斷測試程序
private static Form _OwnerForm; //測試的窗體
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線程
}
//測試用的主線程,循環(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就開啟
//臨時(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ù)字簽名實(shí)現(xiàn)數(shù)據(jù)庫記錄防篡改(Java實(shí)現(xiàn))
本文主要介紹了Java中使用數(shù)字簽名實(shí)現(xiàn)數(shù)據(jù)庫記錄防篡改的方法與步驟。具有很好的參考價(jià)值,下面跟著小編一起來看下吧2017-01-01
DevExpress之ChartControl用法實(shí)例總結(jié)
這篇文章主要介紹了DevExpress之ChartControl用法實(shí)例總結(jié),需要的朋友可以參考下2014-08-08
自定義實(shí)現(xiàn)Json字符串向C#對(duì)象轉(zhuǎn)變的方法
自定義實(shí)現(xiàn)Json字符串向C#對(duì)象轉(zhuǎn)變的方法,需要的朋友可以參考一下2013-03-03
C# char[]與string byte[]與string之間的轉(zhuǎn)換詳解
在本篇文章里小編給大家分享的是關(guān)于C# char[]與string byte[]與string之間的轉(zhuǎn)換的知識(shí)點(diǎn)內(nèi)容,需要的朋友們參考下2019-11-11
C#連接mysql數(shù)據(jù)庫完整實(shí)例
這篇文章主要介紹了C#連接mysql數(shù)據(jù)庫的方法,以一個(gè)完整實(shí)例形式分析了C#操作mysql數(shù)據(jù)庫連接的基本技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-05-05

