.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
david stephan
Gaurav Pal
Post New Web Links

ThreadPool max threads

Posted By:      Posted Date: September 10, 2010    Points: 0   Category :.NET Framework
Hi All, I'm writing an application to do some basic load tests. To start with I wanted to make sure I understood the ThreadPool. Can someone tell me why the below application seems to pause after launching 4 threads on my machine? Thanks Warrick  class Program { static void Main(string[] args) { load doit = new load(); for(int i=0; i < 10; i++) { // ThreadPool.SetMaxThreads = 20; ThreadPool.QueueUserWorkItem(new WaitCallback(doit.dowork),(object)i); } Thread.Sleep(10000); } } class load { public void dowork(object o) { Console.WriteLine("Start "+o.ToString()); Thread.Sleep(1000); Console.WriteLine("Done" + o.ToString()); } }

View Complete Post

More Related Resource Links

ThreadPool problems even after installing fix - Threads are created slowly after they are released



I have a net.tcp service hosted under IIS which is being hit concurrently by 300 clients. After doing a lot of configuration (throttling, maxconnections etc) I show that I have a thread pool problem and requests are not served quickly enough, because thread pool creates new threads every half second. There is no error. My problem is that my clients are not served quickly enough. I configured the thread pool using SetMinThreads but nothing (I also configured SetMaxThreads accordingly). Then I discovered that there is a .NET 3.5 bug and I installed the corresponding fix (http://support.microsoft.com/kb/976898). Now the behaviour I get is the following...

When the 300 clients hit the server, the server opens concurrently 300 threads (I see more than 300 threads for w3wp process in task manager) and clients are served very fast. If after this there are 300 more concurrent clients, they are being served by reusing the previous threads. Excellent!!!

However, if I let the server idle for about 10 seconds, the threads are released! w3wp threads in the task manager drop to around 25. If after this again 300 clients hit the server thread pool starts again to create threads slowly (one every half second)! Why is that?

Running Windows 7 x64, Quad Core, 8 GB RAM

Dimitris Papadimitr

This sharpoint 2010 problem solving threads

Hi friend,

you can use this thread to solving problem and solutions.

Multiple Paramatized Threads



I have a  method that calls two subsequent methods that contain multiple parameters. I would like to run these methods simultaneously and then preform an action based on the return of both methods, so The parent thread cannot continue until BOTH threads have completed. I have tried going trough MSDN examples and haven't got anything to work (the issue is that the parent thread (aka the main method) continues on once one of the thread completes). I have tried cheking the state of threads and waiting the threads, but have not gotten anywhere. Any help would be great. Thanks.

.NET Matters: Ordered Execution With ThreadPool


This month we demonstrate how you can use the ThreadPool to support ordered execution without having to build custom thread pools yourself.

Stephen Toub

MSDN Magazine February 2009

Net Matters: Round-Robin Access To The ThreadPool


Stephen Toub shows you how to add round-robin scheduling support on top of the ThreadPool for more granular processing control.

Stephen Toub

MSDN Magazine January 2009

Pooled Threads: Improve Scalability With New Thread Pool APIs


The new threadpool APIs in Windows Vista and in the upcoming Windows Server 2008 provide advances that even native Windows developers can benefit from.

Robert Saccone

MSDN Magazine October 2007

WPF Threads: Build More Responsive Apps With The Dispatcher


Understanding the Windows Presentation Foundation threading model helps when creating user interfaces that are both compelling and responsive.

Shawn Wildermuth

MSDN Magazine October 2007

Wicked Code: Running ASMX Web Services on STA Threads


Jeff Prosise describes performance problems in an ASMX Web service that relied on legacy COM and Visual Basic 6.0 to perform key processing tasks and the approach he took to find a fix.

Jeff Prosise

MSDN Magazine October 2006

ASP.NET Pipeline: Use Threads and Build Asynchronous Handlers in Your Server-Side Web Code


Fortunately for developers, threading in ASP.NET is a lot easier than it was in ASP. In this article, the author takes a look at threading in the ASP.NET HTTP pipeline, and explains how threads are managed efficiently without the involvement of the developer. The article considers how the common language runtime threadpool is used by ASP.NET to service requests, looks at the pooling mechanisms used for handlers, modules, and applications, and covers both IIS 5.0 and IIS 6.0 and how they differ in their approach to request processing and thread allocation. Finally, how and when to use asynchronous handlers is discussed for developers who still need to use threads in their own applications.

Fritz Onion

MSDN Magazine June 2003

Windows Forms: Give Your .NET-based Application a Fast and Responsive UI with Multiple Threads


If your application performs any non-UI processing on the thread that controls the user interface, it may make the app seem slow and sluggish, frustrating users. But writing multithreaded apps for Windows has long been restricted to C++ developers. Now with the .NET Framework, you can take advantage of multiple threads in C# to control the flow of instructions in your programs and isolate the UI thread for a fast and snappy user interface. This article shows you how. Also, it discusses the pitfalls of multiple threads and presents a framework for safe concurrent thread execution.

Ian Griffiths

MSDN Magazine February 2003

Educational question: How server objects are accessed from multiple client threads?

I will appreciate it if someone can help me to undestand how the following scenario works:   There is remoting server that is registered as WellKnownObjectMode.Singleton. Server implementation: It instantiates a class TestServer that implements interface IFoo interface IFoo { IHelper GetHelper() }   interface IHelper { void StoreString(); }   Implementation of TestServer: internal sealed class TestServer: System.MarshalByRefObject, IFoo { private IHelper> helper = new Helper(); public IHelper GetHelper(string helper){return helper;} }   Implementation of Helper: internal sealed class Helper : System.MarshalByRefObject, IHelper { public void StoreString (string val) {}; }   Implementation of a client (simplified; shown to illustrate the question only): static class Program { public static IFoo TestService; public static IHelper Helper; public TestThreads testmultiplecalls; [STAThread] static void Main() { TestService = (IFoo)Activator.GetObject(typeof(IFoo),"ipc://Channel"); Helper = TestService.GetHelper(); testmultiplecalls.Start(); } }   internal class TestThreads { public Start() { for (int i = 0; i < 20; i++) ThreadPool.QueueUserWorkItem((new TestCall(i.ToString())).DoWork); } }     internal class TestCall { private string m_str; public TestCall (string str) {m_str = str;} public void DoWork() { Program.Helper.StoreString(m_str); } }     Que

ThreadPool with lock isn't thread safe

Hello, I'm having some problems with my code which apparently isn't thread safe at all. The problem is that it doesn't crash often, but of course it does when in production! I have twisted my mind around the problem for a month or so now, and I need some help in sorting out the problem. Well, the problem is that it isn't thread safe, and the collection (_subscribers) is modified while looping. The error says that it crashes in the method: SendRoundSettings. Here's part of the code (which I guess contains the error, as the other methods are pretty much identical): [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)] public class DuplexService : IDuplexService { private readonly object _syncRoot = new object(); private static Subscribers _subscribers = new Subscribers(); private static RoundSettings _currentRoundSettings = new RoundSettings() {CompetitionId = 1, NumberOfAthletesInRound = 0, Round = 0, SortDirection = SortDirection.ByStartNumber}; public void SendJudgeScore(CompetitionJudgeAthleteJump judgeScore) { ThreadPool.QueueUserWorkItem(state => { Subscribers clonedSubscribers = CloneSubscribers(); Console.WriteLine("Sending judgeScore to {0} client(s)", clonedSubscribers.Count(x => x.CompetitionId == judgeScore.CompetitionId));

Exception Propagation across threads.

How can I propagate exception which is being raised in child thread to its parent thread? Please note that child threads could be running asynchronously.

Concurrency: Throttling Concurrency in the CLR 4.0 ThreadPool

The trend toward manycore architectures and parallel apps has resulted in many changes to the CLR 4.0 ThreadPool. If you're emphasizing concurrency in new apps or adding it to old ones, or making use of ASP.NET or Parallel Extension technologies, this is for you.Erika FuentesMSDN Magazine September 2010

Handling Threads in WCF Service

Hi All, I have a WCF serivce which will be called by many clients at the same time. So i set concurrency mode as multiple. So its working fine. In my database i have set time limit for processing each request. So if a particular request is not executing with in that time limit i need to kill(abort) that thread and that request should be given to a new thread. Can any one give me an idea on this that how can i do this? Thanks Lijo.

Producer/Consumer using ThreadPool.RegisterWaitForSingleObject?

Hi, I'm trying to implement an abstract daemon class for a producer/consumer scenario using ThreadPool threads. I came up with: public abstract class ThreadPoolExecuter { private AutoResetEvent _Signal; private bool _IsStopped; protected bool IsStopped { get { return _IsStopped; } } public virtual void Start() { _Signal = new AutoResetEvent(false); ThreadPool.RegisterWaitForSingleObject(_Signal, new WaitOrTimerCallback(Signalled), null, -1, true); } protected void Signal() { _Signal.Set(); } void Signalled(object notUsed, bool timedOut) { if(_IsStopped) return; try { Execute(); } catch(Exception ex) { OnError(ex); } if(!_IsStopped) ThreadPool.RegisterWaitForSingleObject(_Signal, new WaitOrTimerCallback(Signalled), null, -1, true); } protected abstract void Execute(); protected abstract void OnError(Exception ex); } The idea is that different classes could inherit from this, and use their own Execute() implementation depending on their needs. The inheriting class would Signal() whenever it needed to execute some job on the ThreadPool. I have one such inheriting class implementing a Queue for a producer/consumer scenario. My problem is that sometimes my application (a web app hosted on IIS7) seems to miss events. No error is logged. This is obviously a very hazy description of the problem, so what I want to ask is this
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend