最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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> 
  /// 測(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就開啟 
      //臨時(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)文章

最新評(píng)論

丘北县| 裕民县| 阿拉善右旗| 平罗县| 绥中县| 台州市| 广水市| 安达市| 濮阳市| 阳谷县| 安福县| 涪陵区| 宜章县| 乐昌市| 新余市| 财经| 景洪市| 玉环县| 玛纳斯县| 丰都县| 满洲里市| 敦煌市| 扬中市| 奉化市| 罗城| 张家口市| 青州市| 辉南县| 五指山市| 安西县| 金塔县| 招远市| 民丰县| 伊宁市| 呼图壁县| 维西| 安岳县| 眉山市| 西华县| 通许县| 独山县|