A c # multi-thread working queue

Source: Internet
Author: User

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)
{
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.