C # general multi-thread base class, in queue form

Source: Internet
Author: User

We are familiar with the concept of multithreading. For winform developers. A lot of resources are used. however, Timer or backgroundWorker is used. have you ever thought about writing a base class and then .... it can be used immediately when multithreading is used. yes, welfare is coming. I wrote a base class for multithreading for everyone. only you use multithreading, the following Code will certainly help you a lot to View Code 1. from the constructor, a definite list is processed. yes. this multi-thread can only process the identified list. Will you ask. can I add and process them at the same time? (Oh, yes. Please contact the landlord. Of course you can write it yourself, right ?!) Public QueueThreadBase (IEnumerable <T> collection) 2. the Undo function is provided /// <summary> /// Cancel = True /// </summary> public bool Cancel {get; set;} 3. modify the number of threads /// <summary> /// Number of threads /// </summary> public int ThreadCount {get {return this. m_ThreadCount;} set {this. m_ThreadCount = value;} 4. provides a variety of event responses, such as a single completed event, all completed events /// <summary> // all completed events /// </summary> public event Action <CompetedEventArgs> AllCompleted; /// <summary> /// single completion event /// </summary> public event Action <T, CompetedEventArgs> OneCompleted; 5. provided completion percentage // <summary> // completion event data // </summary> public class CompetedEventArgs: EventArgs {public CompetedEventArgs () {}/// <summary> /// completion percentage /// </summary> public int CompetedPrecent {get; set ;} /// <summary> // Exception information // </summary> public Exception InnerException {get; set ;}} 6. the thread termination method is provided, continue/terminate a single thread/terminate all // <summary> // dowork result enumeration // </summary> public enum DoWorkResult {// <summary> /// continue running, default /// </summary> ContinueThread = 0, /// <summary> /// terminate the current thread /// </summary> AbortCurrentThread = 1, /// <summary> // terminate all threads /// </summary> AbortAllThread = 2}

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.