Created
March 21, 2020 13:24
-
-
Save samueleresca/0c49b12247d6214746e86fda3621161e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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