Whether C # multithreading ends can be judged by the thread pool

Source: Internet
Author: User

C # ManualResetEvent Signal status determines whether the thread pool ends

This is an important piece of code, the two-hour study of Piglet, which is recorded for reference.

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading;namespaceend of thread pool conclusion synthesis demo {classProgram { Public Static intICount =0;  Public Static intMaxCount =Ten;//allow up to 10 threads to run in a thread pool        StaticManualResetEvent Eventx =NewManualResetEvent (false);//creates a new ManualResetEvent object and initializes it to a signal-free State        Static voidMain (string[] args) {Console.WriteLine ("ManualResetEvent Initial signal-free status"); Console.WriteLine ("the thread pool has a maximum value of {0} threads. ", MaxCount);  for(intIItem =1; IItem <= MaxCount; iitem++)            {                //inserting a queue elementConsole.WriteLine ("join thread {0}", IItem); ThreadPool.QueueUserWorkItem (NewWaitCallback (FUN1), (Object) IItem); } Console.WriteLine ("wait for the thread pool to complete the operation ... ");//waits for the event to complete, that is, the thread calls the Manualresetevent.set () methodEventx.waitone (Timeout.infinite,true);//the WaitOne () method causes the thread that calls it to wait until the Eventx.set () method is calledConsole.WriteLine ("thread pool ends! ");        Console.ReadLine (); }         Public Static voidFUN1 (Objectj) {Console.WriteLine ("{0} {1}:", Thread.CurrentThread.GetHashCode (), j.tostring ()); Thread.Sleep ( +); Interlocked.Increment (refiCount); if(ICount = =MaxCount)                {Console.WriteLine (); Console.WriteLine ("set ManualResetEvent to signaled state, Setting Eventx");            Eventx.set (); } }}}

Whether C # multithreading ends can be judged by the thread pool

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.