C Multithreaded Processing Method for Multiple Queue Data
- 2021-06-29 11:54:06
- OfStack
This article provides an example of how C#multithreaded processes multiple queue data.Share it for your reference.The implementation is as follows:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Collections;
using System.Windows.Forms;
namespace ThredProcessQueue
{
// Proxy method for presentation typing
public delegate void DelegateShowStateInfo(string state);
/// <summary>
/// Organizer
/// </summary>
public class QueueTester
{
private static bool _Exit = false; // Is the program in progress
private static Form _OwnerForm; // Window
private static DelegateShowStateInfo _StateMethod;
private static IList _Queue1 = new ArrayList(); //Queue1 Of
private static IList _Queue2 = new ArrayList(); //Queue2 Of
private static IList _Queue3 = new ArrayList(); //Queue3 Of
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); // CHINA Queue1 Course
ThreadPool.QueueUserWorkItem(Queue2Thread); // CHINA Queue2 Course
}
// Main process used, along direction 1 Intermediate entry.
public static void MainTestThread(object state)
{
Random R = new Random(1);
double V = 0;
while (_Exit == false)
{
// stay while ( true ) 1 Read the data directly and place it in the queue1 Medium,
// At the same time if queue1 With data, threads 1 On
// Collateral , Collateral
V = R.NextDouble();
_Queue1.Add(V); // Insert column 1
Application.DoEvents();
ShowState();
Thread.Sleep(100);// Generate too fast , See the effect clearly , Stop n Millisecond
}
}
// Yes queue1 The data in is processed and placed in queue2 in
public static void Queue1Thread(object state)
{
while (_Exit == false)
{
while (_Queue1.Count > 0)
{
// Yes queue1 The data in is processed and placed in queue2 in
_Queue2.Add(_Queue1[0]);
_Queue1.RemoveAt(0);
Application.DoEvents();
ShowState();
}
}
}
// Yes queue2 The data in is processed and placed in queue3 in
public static void Queue2Thread(object state)
{
while (_Exit == false)
{
while (_Queue2.Count > 0)
{
// Yes queue1 The data in is processed and placed in queue2 in
_Queue3.Add(_Queue2[0]);
_Queue2.RemoveAt(0);
Application.DoEvents();
ShowState();
}
}
}
// Processes for each column
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
{
}
}
}
}
I hope that the description in this paper will be helpful to everyone's C#program design.