RAPID PUBLISHING ARTICLES PROVIDE INFORMATION DIRECTLY FROM WITHIN THE MICROSOFT SUPPORT ORGANIZATION. THE INFORMATION CONTAINED HEREIN IS CREATED IN RESPONSE TO EMERGING OR UNIQUE TOPICS, OR IS INTENDED SUPPLEMENT OTHER KNOWLEDGE BASE INFORMATION.
Back to the top
In a managed application, a large amount of data is being processed by multiple threads and user interface elements are updated frequently.
Back to the top
The UI may become unresponsive if the number of threads increases significantly over time.
Back to the top
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.Â
Back to the top
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.
Back to the top
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.
Back to the top