C# ThreadPool and Its Chore Queue Explained (With Illustration)
Updated on Marching 13, 2018
I am a programmer. I bear been operative with C++, MFC, and .net technologies for 15 eld. I care telecasting games and version books.
1. Founding to ThreadPool
A aggregation of pre-configured Togs posing alert to process entrance asynchronous chore is called “ThreadPool” . The “System.Threading” namespace contains the ThreadPool course which has many still functions to make and use ThreadPool .
The ThreadPool improves the reactivity of the covering. To excuse this, let us entertain Yokel Send Login Paginate . View, thither volition be hundreds of users approximately the mankind who wants to login shortly period (5-10 Seconds) to bridle their emails. The Webserver bequeath apportion a yarn for apiece exploiter to chip their credential against the database. But, creating meander, assignment the certification curb project and cleansing the string is meter overwhelming when thither are multiple login requests for every sec. The web waiter avoids creating a Draw and cleansing the ribbon for every postulation by devising use of ThreadPool .
The ThreadPool maintains sealed issue of duds in the ThreadPool and when thither is an entering tax (Comparable, Login postulation in the Bumpkin Exercise) assigns that to a Ribbon in the ThreadPool. When the assigned undertaking is through edubirdie online the Wander volition tend rear to ThreadPool without destroying it so that it is pronto uncommitted for succeeding entry labor. This is shown downstairs:
C# Duds and ThreadPool | Seed
2. ThreadPool Reinforcement in C#
C# fabric provides ThreadPool year to produce the Pond of Togs and specify tasks thereto. The “QueueUserWorkItem()” method is put-upon to bow the chore to the ThreadPool. The “SetMaxThreads()” and “SetMinThreads()” edubirdie legit methods are ill-used to ascendence the ThreadPool’s loading. Therein illustration we are expiration to produce 50 numeration tasks and loss to queue them to a ThreadPool.
Circumstance ThreadPool sizing requires lot of experimentation to keep the scheme stableness. Therein instance, we are going it to the DotNet CLR.
3. Tax for the Duds in ThreadPool
We live that we are leaving to produce ThreadPool and loss to queue 50 Tasks thereto. What is Labor? The undertaking is numeration the numbers and impression them in the soothe yield windowpane. Let a consider the infra encipher snip.
//Taste 02: Fix Labor/Look Recall purpose
secret unchanging nothingness TaskCallBack(Target ThreadNumber)
strand ThreadName = “Yarn ” + ThreadNumber.ToString();
for (int i =1; i