1

I am working on a multi-thread Winforms application and I am wondering if there is a recommended number of maximum threads based on the client computer's resources. Thank you.

EDIT - We are using multi-threading to avoid blocking the UI while loading fairly large amount of data to show in Charts. We are showing up to 16 charts simultaneously -16 different threads to load get this data (around 5 calls to the database) at the same time (this will be improved though). Thus we are concerned whether we should limit the number of threads that do work simultaneously. Thank you.

  • Do you have multiple threads because each one of them does different things (that possibly block), or because you're doing one CPU-heavy operation you'd like to spread across multiple cores? – Matti Virkkunen Dec 30 '12 at 23:29
  • Threads are commonly used to reduce CPU idle time. Determining the maximum number of threads really depends on the degree to which your application causes idle CPU time. –  Dec 30 '12 at 23:33
  • 6
    The recommended number of threads is one, it will avoid a lot of really nasty bugs, the kind you'll get into if threading is so unclear that you have to ask this question. Your next best bet is two, particularly the one you get out of a BackgroundWorker. Better advice requires you explaining what you want to do with threads. – Hans Passant Dec 30 '12 at 23:33
  • 2
    possible duplicate of [Maximum number of threads in a .NET app?](http://stackoverflow.com/questions/145312/maximum-number-of-threads-in-a-net-app) – Jeremy Thompson Dec 30 '12 at 23:33
  • I added details based on your comments. Thank you. –  Dec 30 '12 at 23:40
  • 2
    What is your goal here? Maximizing efficiency? Maximizing CPU usage? Minimizing latency? How CPU intensive is each thread going to be? These will all affect the answer. If you don't know yet, use tasks or a thread pool instead -- they're intended to work for 90% of general cases. – Cory Nelson Dec 30 '12 at 23:47

1 Answers1

0

There is no limit in the system or in the .NET Framework.

But what are your goals ? often, you use threads because you want your program works during idle time of the main thread (a background thread). So, in this case, use a GUI thread and a background worker thread. Il you want to do a lot of things at the same moment, use threads, but play with their priority (http://msdn.microsoft.com/fr-fr/library/system.threading.thread.priority.aspx)

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
Stephane Halimi
  • 408
  • 3
  • 5
  • Well, actually there are limits.. lots of them.. but they depend on things like what kind of threads you're using, and how. For instance, if you're using threads from a ThreadPool, then you can quickly run out. – Erik Funkenbusch Dec 30 '12 at 23:50
  • @Stephane -I am using threads to load fairly large amount of data for 16 different chart controls. I am starting these threads simultaneously, each one hits the database about 5 times. So I am wondering if I should limit the number of simultaneous threads. I appreciate your help. –  Dec 31 '12 at 01:16