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

Source: Internet
Author: User

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Threading;

Comprehensive demonstration of namespace thread pool judgment end
{
Class Program
{
public static int iCount = 0;
public static int MaxCount = 10;//allows a thread pool to run up to 10 threads
static ManualResetEvent Eventx = new ManualResetEvent (false); Creates a new ManualResetEvent object and initializes it to a signal-free State

static void Main (string[] args)
{
Console.WriteLine ("ManualResetEvent is initially no signal State");
Console.WriteLine ("The thread pool has a maximum value of {0} threads.") ", MaxCount);
for (int iItem = 1; iItem <= MaxCount; iitem++)
{
Inserting a queue element
Console.WriteLine ("Join thread {0}", IItem);
ThreadPool.QueueUserWorkItem (New WaitCallback (FUN1), (object) iItem);
}
Console.WriteLine ("Wait for the thread pool to complete ... "); Waits for the event to complete, that is, the thread calls the Manualresetevent.set () method
Eventx.waitone (Timeout.infinite, true); The WaitOne () method causes the thread that calls it to wait until the Eventx.set () method is called
Console.WriteLine ("Thread pool end!") ");
Console.ReadLine ();
}

public static void Fun1 (Object j)
{
Console.WriteLine ("{0} {1}:", Thread.CurrentThread.GetHashCode (), j.tostring ());
Thread.Sleep (1000);
Interlocked.Increment (ref ICount);
if (ICount = = MaxCount)
{
Console.WriteLine ();
Console.WriteLine ("Set ManualResetEvent to have signal status, Setting eventx");
Eventx.set ();
}

}

}
}

Go 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.