-1

Recently I am studying and trying C# and I have some confusing points to clear out regarding tasks scheduling in C#.

According to what I learnt, I have understood that task scheduling in C# can be done by using Timer and TaskScheduler.

I have followed these articles: create task scheduler using TaskScheduler class, create task scheduler using Timers

So my confusion is when should we actually use Timer and TaskScheduler for scheduling tasks?

Thank you!

Gaya3
  • 155
  • 2
  • 16
  • 1
    And please help me to improve by giving feedback, If you feel to down vote :) It helps me a lot!! Thank you – Gaya3 Aug 29 '19 at 02:59
  • 1
    Depends if it is long running (days) or not. If so use the Windows Task Scheduler. No additional wasted process always running. No wasted threads. There are nice .NET wrappers around the COM types. –  Aug 29 '19 at 03:21
  • @MickyD thank you for the answer :) so TaskScheduler is good for a process which is long running? or not? – Gaya3 Aug 30 '19 at 01:36
  • 1
    Thanks though it wasn't really an answer like TheGeneral's fine one below. Task Scheduler is great when you have something that needs to start regularly and the program itself shuts down when complete. If the program runs permanently then TS might not be that useful –  Aug 30 '19 at 01:56

1 Answers1

3

Forget about the TaskScheduler class in C#, it's to do with the way tasks are scheduled on the thread-pool, though not to be confused with the Windows Task Scheduler, which runs code on Windows operating system and is agnostic to the language it's written in.

So what you have left are

  1. Timers (which are not really suited for accurate scheduling over long periods of time).
  2. Tasks with delays (which take a lot of work to create actual schedules accurately).
  3. Other fancy things like Reactive Extensions (similar to the above).
  4. job scheduling frameworks (like quartz.net) which give you all the tools you need to schedule tasks to run once or regularly in any time frame and any type of schedule you wish in C#.

For beginners, Timers are probably where you want to be.

halfer
  • 19,824
  • 17
  • 99
  • 186
TheGeneral
  • 79,002
  • 9
  • 103
  • 141