Skip to content

Commit 7f908af

Browse files
authored
Fix: ThreadPool max threads (#2035)
1 parent 9fbbc16 commit 7f908af

File tree

3 files changed

+23
-15
lines changed

3 files changed

+23
-15
lines changed

Source/NETworkManager.Localization/Resources/Strings.Designer.cs

Lines changed: 4 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Source/NETworkManager.Localization/Resources/Strings.resx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3290,10 +3290,9 @@ If the option is disabled again, the values are no longer modified. However, the
32903290
<value>ThreadPool additional min. threads</value>
32913291
</data>
32923292
<data name="HelpMessage_ThreadPoolAdditionalMinThreads" xml:space="preserve">
3293-
<value>This setting sets the minimum number of threads used by the application's ThreadPool. This can improve the performance of e.g. the IP scanner or port scanner.
3294-
The value is added to the default settings.
3295-
3296-
The value 0 leaves the default settings (number of CPU threads).
3293+
<value>This setting specifies the minimum number of threads that will be created from the application's ThreadPool on demand. This can improve the performance for example of the IP scanner or port scanner.
3294+
3295+
The value is added to the default min. threads (number of CPU threads). The value 0 leaves the default settings. If the value is higher than the default max. threads of the ThreadPool, this value is used.
32973296

32983297
If the value is too high, performance problems may occur.
32993298

Source/NETworkManager/App.xaml.cs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ by BornToBeRoot
163163

164164
if (mutexIsAcquired || SettingsManager.Current.Window_MultipleInstances)
165165
{
166+
// Setup background job
166167
if (SettingsManager.Current.General_BackgroundJobInterval != 0)
167168
{
168169
_log.Info($"Setup background job with interval {SettingsManager.Current.General_BackgroundJobInterval} minute(s)...");
@@ -179,14 +180,23 @@ by BornToBeRoot
179180
_log.Info("Background job is disabled.");
180181
}
181182

182-
// Modify the thread pool to increase performance for IP Scanner & Port Scanner
183-
ThreadPool.GetMinThreads(out var workerThreads, out var completionPortThreads);
184-
var setMinThreadsResult = ThreadPool.SetMinThreads(workerThreads + SettingsManager.Current.General_ThreadPoolAdditionalMinThreads, completionPortThreads + SettingsManager.Current.General_ThreadPoolAdditionalMinThreads);
183+
// Setup ThreadPool for the application
184+
ThreadPool.GetMaxThreads(out var workerThreadsMax, out var completionPortThreadsMax);
185+
ThreadPool.GetMinThreads(out var workerThreadsMin, out var completionPortThreadsMin);
185186

186-
if (setMinThreadsResult)
187-
_log.Info($"ThreadPool min threads set to workerThreads: {workerThreads} + {SettingsManager.Current.General_ThreadPoolAdditionalMinThreads}, completionPortThreads: {completionPortThreads} + {SettingsManager.Current.General_ThreadPoolAdditionalMinThreads}.");
187+
var workerThreadsMinNew = workerThreadsMin + SettingsManager.Current.General_ThreadPoolAdditionalMinThreads;
188+
var completionPortThreadsMinNew = completionPortThreadsMin + SettingsManager.Current.General_ThreadPoolAdditionalMinThreads;
189+
190+
if (workerThreadsMinNew > workerThreadsMax)
191+
workerThreadsMinNew = workerThreadsMax;
192+
193+
if (completionPortThreadsMinNew > completionPortThreadsMax)
194+
completionPortThreadsMinNew = completionPortThreadsMax;
195+
196+
if (ThreadPool.SetMinThreads(workerThreadsMinNew, completionPortThreadsMinNew))
197+
_log.Info($"ThreadPool min threads set to: workerThreads: {workerThreadsMinNew}, completionPortThreads: {completionPortThreadsMinNew}");
188198
else
189-
_log.Warn($"ThreadPool min thread could not be set to workerThreads: {workerThreads} + {SettingsManager.Current.General_ThreadPoolAdditionalMinThreads}, completionPortThreads: {completionPortThreads} + {SettingsManager.Current.General_ThreadPoolAdditionalMinThreads}.");
199+
_log.Warn($"ThreadPool min threads could not be set to workerThreads: {workerThreadsMinNew}, completionPortThreads: {completionPortThreadsMinNew}");
190200

191201
// Show splash screen
192202
if (SettingsManager.Current.SplashScreen_Enabled)

0 commit comments

Comments
 (0)