0

I am running background workers to do some processing for me while my app waits for the results. I am currently only using one at a time, but I want to run more than that. Is it safe to run more? How many can I run? Does it matter how much RAM the user has or is it more of a processor thing that controls the running time of the threads?

thanks

my code:

Private check_config_bw As BackgroundWorker = New BackgroundWorker
check_config_bw.WorkerReportsProgress = True
check_config_bw.WorkerSupportsCancellation = True
AddHandler check_config_bw.DoWork, AddressOf check_config_bw_DoWork
AddHandler check_config_bw.ProgressChanged, AddressOf check_config_bw_ProgressChanged
AddHandler check_config_bw.RunWorkerCompleted, AddressOf check_config_bw_RunWorkerCompleted

If Not check_config_bw.IsBusy = True Then
     check_config_bw.RunWorkerAsync()
End If
John
  • 1,310
  • 3
  • 32
  • 58
  • Refer to http://stackoverflow.com/questions/4828296/how-many-threads-can-i-run-concurrently and http://stackoverflow.com/questions/481970/how-many-threads-is-too-many Should answer your question. – SomeNickName Dec 18 '13 at 14:17

2 Answers2

2

I remember reading somewhere a thread occupies 1-2MB of memory + whatever you declare in it, CPU depends on what you do with it. It is safe to open a lot of them, but it makes sense to only probably go x2 number of cores (again, seen this pattern being used often).

You can perform your own performance tests and see for yourself. CPU profiler tool is included with Visual Studio and is very efficient in identifying performance bottlenecks (using it all the time).

enter image description here

Victor Zakharov
  • 25,801
  • 18
  • 85
  • 151
  • But I don't know how many cores the user will have. This app is going to be sold to others, so I can't control the users environment like I can my own. – John Dec 18 '13 at 14:36
  • I've never seen the CPU profiler tool. I'll have to look into that. – John Dec 18 '13 at 14:37
  • 1
    @user2721815: You can query the OS for the number of cores it has. See this: http://stackoverflow.com/questions/1542213/how-to-find-the-number-of-cpu-cores-via-net-c Also check my edit for where to find the profiler in 2010. – Victor Zakharov Dec 18 '13 at 14:37
  • @user2721815: That's quite unfortunate. Well, check your other options then: http://stackoverflow.com/questions/3927/what-are-some-good-net-profilers – Victor Zakharov Dec 18 '13 at 14:48
  • 1
    @Neolisk Thanks for the good links and info on the Performance Wizard. – NoAlias Dec 18 '13 at 16:02
2

You can open as many threads as you want, but always be mindful of system resources. Each thread by default uses 1MB stack space. Also having "too many" threads can lead to excessive context switching which means more time spent on thread housekeeping than actual processing. The OS controls the actual threading (context switching, scheduling, thread priorities, thread quanta, etc..).

Chris O
  • 5,017
  • 3
  • 35
  • 42