Skip to main content

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!

Comments

My Family said…
Nice article pavan, very informative...:) any difference between ControlM and Task scheduler???
I have not worked closely with ControlIM. But on a very high level I could say its advanced workload-automation solution for different operating systems (windows, Linux..etc) and for physical, virtual, and cloud environments. Task scheduler is very simple yet powerful to some extent which is only available on Windows.

ControlIM is used by many enterprise for their unique real-time usage, not only it just run the scheduled jobs, it offeres great features from support perspective too.

Autosys is another such industry wide popular scheduler tool.

Popular posts from this blog

Windows forgets visual effects settings on log-out or reboot for non-administrator account

Steps given here worked for me with Windows 7 SP1. Hope you may find it useful.

Start -> Run-> cmd (press enter) -> type SystemPropertiesPerformance.exeThis gets you the "System Properties" window, go to  Advanced tab -> Performance Settings -> Visual Effects tab and choose "Adjust for best performance" and click OK. Note: While doing all these UAC doesn't show-up as all changes you are doing will be saved in your logged-in profile account.

Reference material used: http://msdn.microsoft.com/en-us/library/ee330866.aspx

Feel free to comment your findings.

Hangfire with In-Memory Storage (ASP.Net MVC 4/5)

This is in continuation to my previous post: Hangfire with ASP.Net MVC : Simplest Code To Start with.

In this post we will see how Hangfire can be configured without the SQL Server as its backend for job scheduling, instead, manage all in-memory

1) Create ASP.Net MVC 4/5 Projects
2) in web.config file
3) Run Install-Package HangFire -Version 1.6.17 or visit here for latest version: https://www.nuget.org/packages/Hangfire/1.6.17
3) Run Install-Package Hangfire.MemoryStorage -Version 1.5.2 or visit here for latest version https://www.nuget.org/packages/Hangfire.MemoryStorage/
4) I am simply updating a table with a primary key (auto incremented), and string field "StampTime" with current date and time.
5) Open Global.asax.cs and paste below shown code.
6) Run the app and check the database.


using Hangfire; using Hangfire.Common; using Hangfire.MemoryStorage; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Web; using…

Hangfire with ASP.Net MVC : Simplest Code To Start with

It seems many are looking for using Hangfire with ASP.Net MVC 4 / 5+ SQL Server here is the simplest code example for you to start quick.

I am using Visual Studio 2017 Community Edition

1) Create ASP.Net MVC 4/5 Projects
2) <add key="owin:AutomaticAppStartup" value="false" /> in web.config file
3)Run Install-Package HangFire -Version 1.6.17 or visit here for latest version: https://www.nuget.org/packages/Hangfire/1.6.17
4) From the sample code, make sure you create database in SQL Server DB and update the connection string.
5) I am simply updating a table with a primary key (auto incremented), and string field "StampTime" with current date and time.
6) Open Global.asax.cs and put this code.
7) Run the app and check the database.

The example code also showcase a hack on how to run jobs with few seconds interval.

namespace HangFireDemo { publicstaticclassMyTasks { publicstaticasync Task<int> MinuteTick() { awa…