Microsoft Knowledge Base Email Alertz

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

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]

Search KbAlertz

Advanced Search

Webmasters
Put kbAlertz on your website.
[ Click Here for more! ]





ASP.NET 2.0 Web Hosting with SQL 2005: Click Here!
Discount ASP.NET Hosting


Bug Tracking Software
For bug tracking software or defect tracking software or issue tracking software, visit Axosoft.


Community Site



We Send hundreds of thousands of emails using ASP.NET Email



Expert Web Design & Graphic Design
Design44.com




Mentioned In








Microsoft Knowledge Base Article

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




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

Article ID:949457
Last Review:February 20, 2008
Revision:1.1
Source: Microsoft Support

Back to the top

RAPID PUBLISHING

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

Action

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

Result

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

Back to the top

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. 

Back to the top

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.

Back to the top

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)

Back to the top

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.

Back to the top


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

Back to the top

Keywords: 
kbnomt kbrapidpub KB949457

Back to the top

       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate

Be the first to leave feedback, to help others about this knowledge base article.

(Optional) Name

(Optional) Public URL Or Email

Comments
No HTML -- Text Only Please