Windows Service or Task Scheduler for maintenance tasks?

前端 未结 6 1497
一生所求
一生所求 2020-12-31 22:41

I have a C# Application that does some maintenance tasks. It needs to run roughly every hour, although it\'s not too important if it\'s a bit off. And it has to run on a Win

相关标签:
6条回答
  • 2020-12-31 23:13

    Windows service is more secure: no one can drop it and ever works. I've found a lot of troubles with Windows Tasks (not executing, ...).

    Windows Task Scheduler is for end-user tasks not for appplication tasks. Anyway, Windows Backup uses it ;-)

    0 讨论(0)
  • 2020-12-31 23:13

    If you go the services route then I would recommend using System.Threading.Timer. With that you can set it to fire an event once an hour. You can put the interval in the app.config if you think you will ever need to change it.

    Services also run under the local system account (by default) whereas you must supply a username/password when using scheduled tasks. If you use your username it becomes a maintenance issue if you ever change your password and forget to update the task.

    I have a situation where I use a combination of both actually. The service runs constantly during the day but they do network and database maintenance every night. So, I use two scheduled tasks. One to shut the service down at midnight and one to turn it back on at 4am. This is done by calling .BAT files with NET STOP/ NET START commands.

    0 讨论(0)
  • 2020-12-31 23:13

    I think a windows service is probably more reliable and easier to monitor, but it won't offer the same scheduling flexibility as a scheduled task out-of-the-box. I'd probably go with the service anyway, since most enterprisey apps tend to run as services, not scheduled tasks.

    For recent projects, I've used Quartz.NET to setup scheduled processing in windows services. It's actually not too hard to setup (good tutorial), and the CronTrigger will give you a ton of flexibility with only a tiny bit of configuration. I like it better than a raw .NET Timer.

    0 讨论(0)
  • 2020-12-31 23:17

    I prefer the Task Scheduler myself, as it is easier to maintain and make changes to the schedule if you need to.

    Also, utilities that run continuously and "sleep" run the risk of causing problems if the developer "forgets" to do things like close connections, files etc. which can build up over time. Having the program "run and exit" is an extra safety blanket. :-)

    0 讨论(0)
  • 2020-12-31 23:22

    You may find this video interesting . Good interview with the engineer responsible for windows services and also goes into when to pick service or task. In a nutshell if your feature falls into the periodic maintenance category go with a task.

    0 讨论(0)
  • 2020-12-31 23:23

    I suppose to throw my 5 cents worth in, using task scheduler will save some memory when your task is not running.

    0 讨论(0)
提交回复
热议问题