Sunday, September 26, 2010

Windows Service or Windows Task Scheduler?

Are you planning to create long running background process (in C#) and thinking to build a Windows service?

Then wait for a moment!


Windows Task scheduler (Start->Programs->Accessories-> System Tools-> Task Scheduler) can also be a decent choice!

"Windows Vista and higher windows (server) operating systems are offering matured Task scheduler with many advancements like Triggers, Actions, Conditions, failure recovery, email notifications, security and much more."

Its obvious that many programmers think Windows Service is easy to maintain and what it can do, task scheduler can’t. But now, it’s time to look at Windows Task Scheduler [also at Microsoft white paper] and see what great features it is offering.

When to go for Windows Service?
Okay, let me try to answer this popular question in one line. 


Windows Service is suitable for receiving service request 'anytime' and process 'instantly'.

Just think of anti-virus scanner, network port activity tracker, remote access connection manager, SQL Server database service, chat server..etc. all these need ‘long running process’ because they have to serve the service requests coming in ‘any time’.

Scheduled Processes
If  

your process wake up every few minutes, queries intended source (like database, Message Queue..etc), performs some operations and goes to sleep
And
Next time when it wakes up, has no interest in knowing what it did previously.
then 
consider leveraging Windows Task Scheduler. Write a console program (hide console window if you want) or WinForms program (with Form + Taskbar visibility false) and set it up with Task scheduler.

Benefits

  • No need to implement timer logic to keep process alive. Having programatic timer unnecessarily consume more CPU cycles because Windows has to create some more threads to keep timer run properly.
  • Avoids doing more R&D on finding and troubleshooting memory leak problems. Windows Scheduler gracefully terminate the windows process (on which our program is running) and creates new one for next run.
  • Don’t have to worry about automatic restart of your process when it crash or to perform multiple retries whenever it encountere problems.
Points to consider...
  • From support point of view, consider using logging component (like log4net) to log events to windows event viewer. Many monitoring systems mostly listen to event viewer and send notification emails to support team to flag about the problem.
  • When setting up your process with Task Scheduler, make sure to check option to run it  even when user is NOT logged into the system.
  • Make sure 'Task Scheduler' service (Start->Run->type 'services.msc' and check) is running on server operating system and set to 'Automatic' mode.
That's all for now. However choice between windows service and task scheduler is a design decision and depends on requirements. If something is tickling in your mind and want to express your thoguts as a comment, feel free to share here. Thanks!