Problem: UI may become unresponsive under heavy load with high threadpool

In a managed application, a large amount of data is being processed by multiple threads and user interface elements are updated frequently.

 

Result

The UI may become unresponsive if the number of threads increases significantly over time.

 

Cause

This is by design. It is due to the uncontrolled number of maximum threads in the threadpool. As the number of threads increases, the chances of lock contention also increase.

When the garbage collector starts a collection, it suspends all the threads executing the managed code so that the objects can be moved safely. So in this type of scenario where we have a large number of contentions, the problem becomes even worse.

 

Resolution

1. [.NET Framework 2.0 and onwards] Put a cap on the number of maxthreads using ICorThreadpool::CorSetMaxThreads (http://msdn2.microsoft.com/en-us/library/aa964877.aspx).
2. Use BeginInvoke (http://msdn2.microsoft.com/en-us/library/system.windows.forms.control.begininvoke.aspx) instead of Invoke (http://msdn2.microsoft.com/en-us/library/system.windows.forms.control.invoke.aspx) for the asynchronous call on the thread.

Threadpool and maximum number of threads

How many threadpools?
The number of threadpools should ideally be equal to number of CPUs the system has.

How many threads in the pool?
In .Net Framework 1.1, the threadpool will put a limit on the number of threads it allows in the process. By default this value is 25 worker threads and 25 IOCompletion threads. This value cannot be changed in .Net Framework 1.1

We can retrieve this number by using GetMaxThreads (http://msdn2.microsoft.com/en-us/library/system.threading.threadpool.getmaxthreads.aspx) method.

What is new?
In .Net Framework 2.0 and later, we can change the maximum number of threads in the threadpool by calling CorSetMaxThreads (http://msdn2.microsoft.com/en-us/library/aa964877.aspx)(…). This function takes two parameters :

1. MaxWorkerThreads – The maximum number of worker threads in the thread pool.
2. MaxIOCompletionThreads – The maximum number of asynchronous I/O threads in the thread pool.

Invoke Vs. BeginInvoke
The BeginInvoke method invokes a delegate asynchronously on the thread. Each window on the system is owned by a thread, and that thread is responsible for ensuring that the thread is constantly pumping messages for the window. If you need your UI thread to complete before proceeding, you can use Invoke. But if this is not your requirement, you should use BeginInvoke. Invoke (http://msdn2.microsoft.com/en-us/library/system.windows.forms.control.invoke.aspx) is similar to SendMessage and BeginInvoke (http://msdn2.microsoft.com/en-us/library/system.windows.forms.control.begininvoke.aspx) is similar to PostMessage.

BeginInvoke(…) is faster and can limit UI contention.

 

More Information

See Also :
ICorThreadpool::CorSetMaxThreads Method (http://msdn2.microsoft.com/en-us/library/aa964877.aspx)Control.BeginInvoke Method (http://msdn2.microsoft.com/en-us/library/system.windows.forms.control.begininvoke.aspx) (http://msdn2.microsoft.com/en-us/library/system.windows.forms.control.begininvoke(VS.71).aspx)Performance-Conscious Thread Synchronization (http://msdn.microsoft.com/msdnmag/issues/05/10/ConcurrentAffairs/)
Garbage Collection—Part 2: Automatic Memory Management in the Microsoft .NET Framework (http://msdn.microsoft.com/msdnmag/issues/1200/GCI2/)Programming the Thread Pool in the .NET Framework (http://msdn2.microsoft.com/en-us/library/ms973903.aspx)

 

DISCLAIMER

MICROSOFT AND/OR ITS SUPPLIERS MAKE NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY, RELIABILITY OR ACCURACY OF THE INFORMATION CONTAINED IN THE DOCUMENTS AND RELATED GRAPHICS PUBLISHED ON THIS WEBSITE (THE “MATERIALS”) FOR ANY PURPOSE. THE MATERIALS MAY INCLUDE TECHNICAL INACCURACIES OR TYPOGRAPHICAL ERRORS AND MAY BE REVISED AT ANY TIME WITHOUT NOTICE.

TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, MICROSOFT AND/OR ITS SUPPLIERS DISCLAIM AND EXCLUDE ALL REPRESENTATIONS, WARRANTIES, AND CONDITIONS WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING BUT NOT LIMITED TO REPRESENTATIONS, WARRANTIES, OR CONDITIONS OF TITLE, NON INFRINGEMENT, SATISFACTORY CONDITION OR QUALITY, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, WITH RESPECT TO THE MATERIALS.

 


APPLIES TO
Microsoft .NET Framework 1.1
Microsoft .NET Framework 2.0
Microsoft .NET Framework 3.0
Microsoft .NET Framework 3.5

——————————————–

Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
Microsoft Corporation. All rights reserved. Terms of Use | Trademarks


You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

AddThis Social Bookmark Button

Leave a Reply

*
To prove that you're not a bot, enter this code
Anti-Spam Image