How to do it...

  1. Create a new method called IncreaseThreadPoolSize() in the Demo class.
  1. Start by adding the code to read the number of processors on the current machine using Environment.ProcessorCount:
        public class Demo
{
public void IncreaseThreadPoolSize()
{
int numberOfProcessors = Environment.ProcessorCount;
WriteLine($"Processor Count = {numberOfProcessors}");
}
}
  1. Next, we retrieve the maximum and minimum threads available in the thread pool:
        int maxworkerThreads; 
int maxconcurrentActiveRequests;
int minworkerThreads;
int minconcurrentActiveRequests;
ThreadPool.GetMinThreads(out minworkerThreads,
out minconcurrentActiveRequests);
WriteLine($"ThreadPool minimum Worker = {minworkerThreads}
and minimum Requests = {minconcurrentActiveRequests}");
ThreadPool.GetMaxThreads(out maxworkerThreads,
out maxconcurrentActiveRequests);
WriteLine($"ThreadPool maximum Worker = {maxworkerThreads}
and maximum Requests = {maxconcurrentActiveRequests}");
  1. Then, we generate a random number between the maximum and minimum number of threads in the thread pool:
        Random rndWorkers = new Random(); 
int newMaxWorker = rndWorkers.Next(minworkerThreads,
maxworkerThreads);
WriteLine($"New Max Worker Thread generated = {newMaxWorker}");

Random rndConRequests = new Random();
int newMaxRequests = rndConRequests.Next(
minconcurrentActiveRequests, maxconcurrentActiveRequests);
WriteLine($"New Max Active Requests generated = {newMaxRequests}");
  1. We now need to attempt to set the maximum number of threads in the thread pool by calling the SetMaxThreads method, and setting it to our new random maximum value for the worker threads and the completion port threads. Any requests above this maximum number will be queued until the thread pool threads become active again. If the SetMaxThreads method is successful, the method will return true; otherwise, it will return false. It is a good idea to ensure that the SetMaxThreads method is successful:
        bool changeSucceeded = ThreadPool.SetMaxThreads(
newMaxWorker, newMaxRequests);
if (changeSucceeded)
{
WriteLine("SetMaxThreads completed");
int maxworkerThreadCount;
int maxconcurrentActiveRequestCount;
ThreadPool.GetMaxThreads(out maxworkerThreadCount,
out maxconcurrentActiveRequestCount);
WriteLine($"ThreadPool Max Worker = {maxworkerThreadCount}
and Max Requests = {maxconcurrentActiveRequestCount}");
}
else
WriteLine("SetMaxThreads failed");
Worker threads is the maximum number of worker threads in the thread pool while the completion port threads is the maximum number of asynchronous I/O threads in the thread pool.
  1. When you've added all the code in the steps listed, your IncreaseThreadPoolSize() method should look like this:
        public class Demo
{
public void IncreaseThreadPoolSize()
{
int numberOfProcessors = Environment.ProcessorCount;
WriteLine($"Processor Count = {numberOfProcessors}");

int maxworkerThreads;
int maxconcurrentActiveRequests;
int minworkerThreads;
int minconcurrentActiveRequests;
ThreadPool.GetMinThreads(out minworkerThreads,
out minconcurrentActiveRequests);
WriteLine($"ThreadPool minimum Worker = {minworkerThreads}
and minimum Requests = {minconcurrentActiveRequests}");
ThreadPool.GetMaxThreads(out maxworkerThreads,
out maxconcurrentActiveRequests);
WriteLine($"ThreadPool maximum Worker = {maxworkerThreads}
and maximum Requests = {maxconcurrentActiveRequests}");

Random rndWorkers = new Random();
int newMaxWorker = rndWorkers.Next(minworkerThreads,
maxworkerThreads);
WriteLine($"New Max Worker Thread generated = {newMaxWorker}");

Random rndConRequests = new Random();
int newMaxRequests = rndConRequests.Next(
minconcurrentActiveRequests,
maxconcurrentActiveRequests);
WriteLine($"New Max Active Requests generated =
{newMaxRequests}");

bool changeSucceeded = ThreadPool.SetMaxThreads(
newMaxWorker, newMaxRequests);
if (changeSucceeded)
{
WriteLine("SetMaxThreads completed");
int maxworkerThreadCount;
int maxconcurrentActiveRequestCount;
ThreadPool.GetMaxThreads(out maxworkerThreadCount,
out maxconcurrentActiveRequestCount);
WriteLine($"ThreadPool Max Worker = {maxworkerThreadCount}
and Max Requests = {maxconcurrentActiveRequestCount}");
}
else
WriteLine("SetMaxThreads failed");

}
}
  1. Head on over to your console application, create a new instance of your Demo class, and call the IncreaseThreadPoolSize() method:
        Demo oRecipe = new Demo(); 
oRecipe.IncreaseThreadPoolSize();
Console.ReadLine();
  1. Finally, run your console application and take note of the output:
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.21.98.207