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

C#多線程處理多個隊列數(shù)據(jù)的方法

 更新時間:2015年07月03日 10:16:35   作者:程序猴  
這篇文章主要介紹了C#多線程處理多個隊列數(shù)據(jù)的方法,涉及C#線程與隊列的相關(guān)操作技巧,需要的朋友可以參考下

本文實例講述了C#多線程處理多個隊列數(shù)據(jù)的方法。分享給大家供大家參考。具體實現(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); //啟動Queue1線程 
     ThreadPool.QueueUserWorkItem(Queue2Thread); //啟動Queue2線程 
   }
   //測試用的主線程,循環(huán)向隊列1中壓入數(shù)據(jù)。 
   public static void MainTestThread(object state)
   {
     Random R = new Random(1);
     double V = 0;
     while (_Exit == false)
     {
      //在while(true)里一直對數(shù)據(jù)進(jìn)行讀取,然后放到queue1中, 
      //與此同時如果queue1中有數(shù)據(jù),則線程1就開啟 
      //臨時數(shù)據(jù),隨機數(shù) 
      V = R.NextDouble();
      _Queue1.Add(V); //把數(shù)據(jù)插入到隊列1 
      Application.DoEvents();
      ShowState();
      Thread.Sleep(100);//生成隨機數(shù)太快,為了看清效果,暫停n毫秒 
     }
   }
   
   //對queue1中的數(shù)據(jù)進(jìn)行處理,處理后放到queue2中 
   public static void Queue1Thread(object state)
   {
     while (_Exit == false)
     {
      while (_Queue1.Count > 0)
      {
        //對queue1中的數(shù)據(jù)進(jìn)行處理,處理后放到queue2中 
        _Queue2.Add(_Queue1[0]);
        _Queue1.RemoveAt(0);
        Application.DoEvents();
        ShowState();
      }
     }
   }
   //對queue2中的數(shù)據(jù)進(jìn)行處理,處理后放到queue3中 
   public static void Queue2Thread(object state)
   {
     while (_Exit == false)
     {
      while (_Queue2.Count > 0)
      {
        //對queue1中的數(shù)據(jù)進(jìn)行處理,處理后放到queue2中 
        _Queue3.Add(_Queue2[0]);
        _Queue2.RemoveAt(0);
        Application.DoEvents();
        ShowState();
      }
     }
   }
   //用于監(jiān)視各隊列狀態(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
     {
     }
   }
  }
}

希望本文所述對大家的C#程序設(shè)計有所幫助。

相關(guān)文章

最新評論

古田县| 雅安市| 卓资县| 济南市| 社旗县| 沭阳县| 麻江县| 临猗县| 广东省| 平塘县| 廉江市| 武冈市| 临湘市| 镇沅| 永靖县| 尼玛县| 会同县| 平原县| 娱乐| 锡林浩特市| 巴中市| 焉耆| 徐州市| 罗平县| 淮北市| 修武县| 天水市| 闵行区| 抚州市| 芦溪县| 汕头市| 涡阳县| 鸡西市| 刚察县| 仁怀市| 申扎县| 乐都县| 沂南县| 南靖县| 城固县| 城口县|