Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save samueleresca/0c49b12247d6214746e86fda3621161e to your computer and use it in GitHub Desktop.
Save samueleresca/0c49b12247d6214746e86fda3621161e to your computer and use it in GitHub Desktop.
using System;
using System.Threading;
using System.Threading.Tasks;
namespace Blog.NotesOnThreading
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(Environment.ProcessorCount);
ThreadPool.SetMinThreads(8, 8);
while (true)
{
ThreadPool.QueueUserWorkItem(
arg =>
{
Process();
});
Thread.Sleep(10);
}
}
static void Process()
{
var completionFlag = new ManualResetEventSlim(false);
ThreadPool.QueueUserWorkItem(
evt =>
{
Thread.Sleep(1000);
evt.Set();
}, completionFlag, preferLocal: true);
completionFlag.Wait();
Console.WriteLine($"{nameof(Process)} completed - {DateTime.Now.ToLongTimeString()} " +
$"| Pending work count: {ThreadPool.PendingWorkItemCount}");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment