1

I was wondering if there was a reasonable way to determine the load capabilities of the Timers.Timer object? Or, rather, the thread capabilities. Does anybody know if there are any issues with spinning up a high number(in the hundreds) of threads using the Timer class?

EDIT: For work, we will be spinning up a number of threads that will do a threshold check and give points based on whoever meets a threshold. It will spin up a thread per client, and will check every 5 minutes or so. It's a service, and it will be installed on the server, hence why I'm using Timers.Timer instead of the Threading.Timer(as I also heard that Timers.Timer is more thread-safe than Threading.Timers). Currently, I will only have up ~10 Timers, as that's all I need to handle. Am asking for possible future load. Was also using this post as a reference for which timer to use: System.Timers.Timer vs System.Threading.Timer

Community
  • 1
  • 1
Michael Fender
  • 551
  • 2
  • 6
  • 15
  • 3
    You should not spin up hundreds of threads, regardless of how you do it. Period. When you do that, you add overhead for each additional thread regarding context switching and memory pressure. – Lasse V. Karlsen Oct 25 '13 at 13:21
  • There's Windows.Forms.Timer and System.Threading.Timer, Pick one :) Also, what will these threads be doing? Spinning up hundreds of threads only is logic in some cases... – Stormenet Oct 25 '13 at 13:21
  • @Stormenet none,OP stated in title Timers.Timer :) – terrybozzio Oct 25 '13 at 13:24
  • @terry I wasn't aware of that timer, interesting :) – Stormenet Oct 25 '13 at 13:27
  • @Stormenet DispatcherTimer also available – Sriram Sakthivel Oct 25 '13 at 13:28
  • @sriram, it seems that the number of timers at my disposal has doubled :p – Stormenet Oct 25 '13 at 13:29
  • @Stormenet whats even more interesting is this article take a look,http://msdn.microsoft.com/en-us/magazine/cc164015.aspx – terrybozzio Oct 25 '13 at 13:31
  • Spawning a large number of threads is not a good idea. It might be better to look at why you are spawning these threads or consider using the Threadpool instead. – Kami Oct 25 '13 at 13:33
  • I've edited my original post to hopefully explain why I'm doing it(or will possibly be doing it). Never realized the Threadpool class, though. Will take a look into it. – Michael Fender Oct 25 '13 at 14:23
  • I see several comments that you should "never" spin a large number of threads. I vehemently disagree. In a real-world, production environment (i.e. outside a classroom) it IS often necessary to use hundreds of concurrent treads. In fact, pretty much any game these days does just that. – Christopher J Smith Oct 25 '13 at 14:34

1 Answers1

3

Does anybody know if there are any issues with spinning up a high number(in the hundreds) of threads using the Timer class?

You seem to assume that each timer will create a private thread. However, that is not the case. The timer callbacks are executed on the .NET thread pool. So creating many timers in itself is not an issue but there are obviously limits to how much concurrent work the thread pool can handle. This will affect you if all the timers fire at the same time.

Microsoft provides some general information about the thread pool in the article The Managed Thread Pool.

Martin Liversage
  • 104,481
  • 22
  • 209
  • 256
  • you are right but op is talking about using Timers.Timer not threading.timer :) – terrybozzio Oct 25 '13 at 13:58
  • @terrybozzio: The class `System.Timers.Timer` is implemented using `System.Threading.Timer` so no matter what timer you use you get the same underlying implementation. – Martin Liversage Oct 25 '13 at 14:03
  • not the same underlying implementation please take a look, http://msdn.microsoft.com/en-us/magazine/cc164015.aspx – terrybozzio Oct 25 '13 at 14:13
  • Huh. Okay. And that will possibly be the issue(where I'm firing all the timers at roughly the same time). – Michael Fender Oct 25 '13 at 14:24
  • @terrybozzio: If you decompile `System.Timers.Timer` or look at the [source code](http://www.dotnetframework.org/default.aspx/DotNET/DotNET/8@0/untmp/whidbey/REDBITS/ndp/fx/src/Services/Timers/System/Timers/Timer@cs/1/Timer@cs) you will see that it contains a `System.Threading.Timer` field that is used as the underlying implementation while providing some functionality on top. – Martin Liversage Oct 25 '13 at 15:10
  • yes some but not "get the same underlying implementation",by your answer did you at least read the article?...again i am not desrespecting your answer. – terrybozzio Oct 25 '13 at 15:11
  • @terrybozzio: My answer applies to both `System.Timers.Timer` and `System.Threading.Timer` - not just `System.Threading.Timer` as you first comment seem to imply even though my answer only mentions "timers" and not a specific timer class. So my comments are simply an attempt to clarify that. It has nothing to do with "disrespect". – Martin Liversage Oct 25 '13 at 15:44