// 顯示執行緒集區內的所有運作參數 public static void ShowAllThreadPoolInformation(ThreadPoolInformation threadPoolInformation) { Console.WriteLine($"執行緒集區的相關設定資訊"); Console.WriteLine($"邏輯處理器數目 : {threadPoolInformation.ProcessorCount} "); Console.WriteLine($"WorkItem Thread :" + $" (Busy:{threadPoolInformation.BusyWorkerThreads}, Free:{threadPoolInformation.AvailableWorkerThreads}, Min:{threadPoolInformation.MinWorkerThreads}, Max:{threadPoolInformation.MaxWorkerThreads})"); Console.WriteLine($"IOPC Thread :" + $" (Busy:{threadPoolInformation.BusyCompletionPortThreads}, Free:{threadPoolInformation.AvailableCompletionPortThreads}, Min:{threadPoolInformation.MinCompletionPortThreads}, Max:{threadPoolInformation.MaxCompletionPortThreads})"); Console.WriteLine($""); }
// 顯示執行緒集區內上有多少空間,可以用來增加新執行緒的數量 public static void ShowAvailableThreadPoolInformation(ThreadPoolInformation threadPoolInformation) { if (LastBusyWorkerThreads != threadPoolInformation.BusyWorkerThreads || LastBusyCompletionPortThreads != threadPoolInformation.BusyCompletionPortThreads) { LastBusyWorkerThreads = threadPoolInformation.BusyWorkerThreads; LastBusyCompletionPortThreads = threadPoolInformation.BusyCompletionPortThreads; //Console.WriteLine($" WorkItem Thread :" + // $" (Busy:{threadPoolInformation.BusyWorkerThreads}, Free:{threadPoolInformation.AvailableWorkerThreads}, Min:{threadPoolInformation.MinWorkerThreads}, Max:{threadPoolInformation.MaxWorkerThreads})"); //Console.WriteLine($" IOPC Thread :" + // $" (Busy:{threadPoolInformation.BusyCompletionPortThreads}, Free:{threadPoolInformation.AvailableCompletionPortThreads}, Min:{threadPoolInformation.MinCompletionPortThreads}, Max:{threadPoolInformation.MaxCompletionPortThreads})"); } }
public static void ShowCurrentThreadUsage(ThreadPoolInformation threadPoolInformation, ThreadPoolInformation threadPoolCurrentInformation) { int workerThreads; int completionPortThreads; // 傳回之執行緒集區的現在還可以容許使用多少的執行緒數量大小 ThreadPool.GetAvailableThreads(out workerThreads, out completionPortThreads); threadPoolCurrentInformation.AvailableWorkerThreads = workerThreads; threadPoolCurrentInformation.AvailableCompletionPortThreads = completionPortThreads; threadPoolCurrentInformation.BusyWorkerThreads = threadPoolInformation.AvailableWorkerThreads - workerThreads; threadPoolCurrentInformation.BusyCompletionPortThreads = threadPoolInformation.AvailableCompletionPortThreads - completionPortThreads; ShowAvailableThreadPoolInformation(threadPoolCurrentInformation); }
public static void BeginMonitor() { threadPoolInformation = new ThreadPoolInformation(); GetThreadPoolInformation(threadPoolInformation); //LastBusyWorkerThreads = threadPoolInformation.BusyWorkerThreads; //LastBusyCompletionPortThreads = threadPoolInformation.BusyCompletionPortThreads; ShowAllThreadPoolInformation(threadPoolInformation); threadPoolCurrentInformation = threadPoolInformation.Clone(); while (true) { ShowCurrentThreadUsage(threadPoolInformation, threadPoolCurrentInformation); Thread.Sleep(0); } }
// 取得執行緒集區內的相關設定參數 public static void GetThreadPoolInformation(ThreadPoolInformation threadPoolInformation) { int workerThreads; int completionPortThreads; // 傳回之執行緒集區的現在還可以容許使用多少的執行緒數量大小 ThreadPool.GetAvailableThreads(out workerThreads, out completionPortThreads); threadPoolInformation.AvailableWorkerThreads = workerThreads; threadPoolInformation.AvailableCompletionPortThreads = completionPortThreads; // 擷取可並行使用之執行緒集區的要求數目。 超過該數目的所有要求會繼續佇列,直到可以使用執行緒集區執行緒為止 ThreadPool.GetMaxThreads(out workerThreads, out completionPortThreads); threadPoolInformation.MaxWorkerThreads = workerThreads; threadPoolInformation.MaxCompletionPortThreads = completionPortThreads; // 在切換至管理執行緒建立和解構的演算法之前,擷取執行緒集區隨著提出新要求,視需要建立的執行緒最小數目。 ThreadPool.GetMinThreads(out workerThreads, out completionPortThreads); threadPoolInformation.MinWorkerThreads = workerThreads; threadPoolInformation.MinCompletionPortThreads = completionPortThreads; // 如果目前電腦包含多個處理器群組,則這個屬性會傳回可供 Common Language Runtime (CLR) 使用的邏輯處理器數目 threadPoolInformation.ProcessorCount = System.Environment.ProcessorCount; }
public void ComputeBusyThreads(ThreadPoolInformation threadPoolInformation) { this.BusyWorkerThreads = threadPoolInformation.AvailableWorkerThreads - this.AvailableWorkerThreads; this.BusyCompletionPortThreads = threadPoolInformation.BusyCompletionPortThreads - this.BusyCompletionPortThreads; }