Which one is better to use in a Windows, Thread, or Timer service?

Hi, I am new to Windows service. I developed one Windows service using threads that create a new thread in OnStart (), and I run methods using this thread and use a sleep thread for the next start, but one of my friends said that it is better to use a timer than a thread. so I was wondering which one is best to do? thanks for the help

+4
source share
2 answers

Based on your comment, your goal is to run specific code at specific intervals:

Conceptually, a timer is better than disabling a stream. This requires timers. If you choose a sleep approach with a thread, I think that in practice it will work fine, although it is better to use timers.

There is a third approach - using the task scheduler. You can use the Windows task scheduler or the more powerful quartz.net ( nuget package here ).


It really is a question of how you value the semantic correctness of pragmatism.

  • From the point of view of semantic correctness, the best approach is to use the task scheduler, since you really want to schedule work.
  • From a pragmatic point of view, the best approach is probably to simply continue using the service already developed , even if it causes the thread to sleep and spend its time on something other than changing fully working code.

More Views on Timer and Task Scheduler: Best Timer to Use in a Windows Service

+3
source

It depends on what you do, I suppose. The difference between Timer and Thread is that Timer uses a thread from the thread pool, while Thread allocates a new one for the task.

You can read more here: http://social.msdn.microsoft.com/Forums/vstudio/en-US/c5b0e037-ccb5-42c0-bb0a-304572c8c8d2/timer-vs-thread-performance

0
source

Source: https://habr.com/ru/post/1494935/


All Articles