-2

I want to know will System.timer.timer create a new Thread and running on it. From what I observed, when I running the debug mode on my program, the timer1_Elapsed is still running on the main thread. Can anyone help me on that?

  • the Winforms Timer runs on the UI Thread. The System.Timers.Timer runs on an available ThreadPool thread. – Charles Dec 14 '19 at 02:50
  • @Charles, hi, I'm new to C#, what does avaliable ThreadPool thread mean? I'm using Timers.Timer,but it's running on the Main thread. And I also have a Form.Timer which should running on the UI thread, but in the debug mode, I still found it's running on the main thread. Can you explain it to me ? – oocharlesxx Dec 14 '19 at 02:59
  • Welcome to Timer hell. Here is a old one: https://stackoverflow.com/questions/10317088/why-there-are-5-versions-of-timer-classes-in-net | But my last count was 7-ish? | Some always start a new thread, some never start a new thread and some only sometimes start one - it varries from version to Version. http://www.abhisheksur.com/2011/03/all-about-net-timers-comparison.html – Christopher Dec 14 '19 at 03:17
  • In Windows Forms you usually use the WinForms Timer. It raises the Tick even in the Event Queue, so little issue with cross Threading. It does not have metronome quality, however. | It really depends what you are planning to do, one what elements you plan to do it, how often you plan to do it, how important second precision is, etc. – Christopher Dec 14 '19 at 03:18
  • The thread pool is a pool of threads that creates and reuses threads as needed. You can use functions like `ThreadPool.GetMinThreads` to get some information. Functions like : `Task.Factory.StartNew, Parallel.For, Task.Run, System.Timers.Timer, ThreadPool.QueueUserWorkItem` automatically use the thread pool threads. The thread pool makes multithreading a little bit easier, because you no longer have to manually create and manage threads. The thread pool will create more threads if you have more CPU Cores available, so your program automatically scales on stronger PCs. – Charles Dec 14 '19 at 03:22
  • if you need a dedicated timer then its better to create a new Thread() and run your code inside with `while(true) { code(); Thread.Sleep(intervalMs); }` – Charles Dec 14 '19 at 03:27

1 Answers1

2

There are lots of Timers in .NET. IIRC crossed the half dozen last decade and are now approaching a dozen. If you include stuff like StopWatches, Thread/Task.Delay and the like, we already broke the dozen. And we might have broken the Dozen even without that - it is hard to keep a current list.

According to this article, System.Timers.Timer uses the ThreadPool. Thread pools are realy just automagic ways of managing the creation and reusing of Threads to handle all kinds of tasks.

Generally you want to avoid Multithreading, however. With GUI's this will run you head-first into CrossThreadExceptions and the need to invoke.

I find it important to differentiate between Multitasking and Multithreading. Multitasking is the Umbrella term. Multithreading is only required if you got heavy CPU load, like calculations. However for a long time, Multithreading was just a quickest, lowest code way to implement Multitasking. This has changed recently, as we now got stuff like Async and Await, to do Threadless Multitasking with very low code footprint.

Christopher
  • 9,634
  • 2
  • 17
  • 31