using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; namespace SuperSocket.Common { /// /// Thread pool extension class /// public static class TheadPoolEx { /// /// Resets the thread pool. /// /// The max working threads. /// The max completion port threads. /// The min working threads. /// The min completion port threads. /// public static bool ResetThreadPool(int? maxWorkingThreads, int? maxCompletionPortThreads, int? minWorkingThreads, int? minCompletionPortThreads) { if (maxWorkingThreads.HasValue || maxCompletionPortThreads.HasValue) { int oldMaxWorkingThreads, oldMaxCompletionPortThreads; ThreadPool.GetMaxThreads(out oldMaxWorkingThreads, out oldMaxCompletionPortThreads); if (!maxWorkingThreads.HasValue) maxWorkingThreads = oldMaxWorkingThreads; if (!maxCompletionPortThreads.HasValue) maxCompletionPortThreads = oldMaxCompletionPortThreads; if (maxWorkingThreads.Value != oldMaxWorkingThreads || maxCompletionPortThreads.Value != oldMaxCompletionPortThreads) { if (!ThreadPool.SetMaxThreads(maxWorkingThreads.Value, maxCompletionPortThreads.Value)) return false; } } if (minWorkingThreads.HasValue || minCompletionPortThreads.HasValue) { int oldMinWorkingThreads, oldMinCompletionPortThreads; ThreadPool.GetMinThreads(out oldMinWorkingThreads, out oldMinCompletionPortThreads); if (!minWorkingThreads.HasValue) minWorkingThreads = oldMinWorkingThreads; if (!minCompletionPortThreads.HasValue) minCompletionPortThreads = oldMinCompletionPortThreads; if (minWorkingThreads.Value != oldMinWorkingThreads || minCompletionPortThreads.Value != oldMinCompletionPortThreads) { if (!ThreadPool.SetMinThreads(minWorkingThreads.Value, minCompletionPortThreads.Value)) return false; } } return true; } } }