Skip to content

Instantly share code, notes, and snippets.

@Entroper
Created July 26, 2017 16:31
Show Gist options
  • Save Entroper/d9b517c2230a892a1c87c9751272e55d to your computer and use it in GitHub Desktop.
Save Entroper/d9b517c2230a892a1c87c9751272e55d to your computer and use it in GitHub Desktop.
Shows how the .NET Threadpool creates new threads.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(GetActiveThreads());
while (true)
{
Thread.Sleep(100);
Task.Run(() => Thread.Sleep(TimeSpan.FromMinutes(10)));
Console.WriteLine(GetActiveThreads());
}
}
static int GetActiveThreads()
{
ThreadPool.GetMaxThreads(out int maxWorker, out int maxIo);
ThreadPool.GetAvailableThreads(out int worker, out int io);
return maxWorker - worker;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment