Write a multi-threaded working queue to automatically process objects in the queue. Multiple Threads add elements to the queue.
The WorkSequential attribute can be set to implement Single-thread (strictly ordered) or multi-thread (in order,
Multi-threaded processing, does not guarantee smooth processing of queue elements.
Source code:
Code
***************
* This working queue ensures thread security.
*
*
*
*
********/
Namespace WorkQueue
{
Using System. Collections. Generic;
Using System;
Using System. Threading;
Public delegate void UserWorkEventHandler <T> (object sender, WorkQueue <T>. EnqueueEventArgs e );
Public class WorkQueue <T>
{
Private bool IsWorking; // indicates whether the processing thread is working.
Private object lockIsWorking = new object (); // synchronization object for IsWorking
Private Queue <T> queue; // actual Queue
Private object lockObj = new object (); // queue synchronization object
/// <Summary>
/// Bind the user to the item object in the queue
/// Operation Event
/// </Summary>
Public event UserWorkEventHandler <T> UserWork;
Public WorkQueue (int n)
{
Queue = new Queue <T> (n );
}
Public WorkQueue ()
{
Queue = new Queue <T> ();
}
/// <Summary>
/// Use this function with caution,
/// Only ensure this moment, the queue value is blank
/// </Summary>
/// <Returns> </returns>
Public bool IsEmpty ()
{
Lock (lockObj)
{