views:

384

answers:

4

I have a task that needs to run every 30 seconds. I can do one of two things:

  1. Write a command line app that runs the task once, waits 30 seconds, runs it again and then exits. I can schedule this task with Scheduled Tasks in Windows to run every minute

  2. Write a Service that runs a task repeatedly while waiting 30 seconds in between each run.

Number 1 is more trivial, in my opinion and I would opt to do it this way by default. Am I wimping out? Is there a reason why I should make this a Service and not a scheduled task? What are the pros and cons of both and which would you pick in the end?

+3  A: 

If you're trying to run every 30 seconds, I'd go for option 2. This is pretty much a continually running job, in that case. The overhead of starting and stopping the process is probably higher than the process itself, especially if you use an appropriate timer.

If you make a job that is running once a day (or a few times a day), then I'd go for option 1 - using a scheduled task.

Reed Copsey
+2  A: 

The task scheduler in windows seems a bit flakey in my opinion. I think you would get a more reliable result running as a service.

Also, a service could keep resources in memory, such as reading input from a file, and only have to do this at start-up of the service, not every 30 seconds.

benPearce
+5  A: 

I read a nice blog post about this question recently. It goes into a lot of good reasons why you should not write a service to run a recurring job. Additionally, this question has been asked before:

http://stackoverflow.com/questions/390307/windows-service-vs-scheduled-task http://stackoverflow.com/questions/767490/windows-service-or-scheduled-task-which-one-do-we-prefer

One advantage of using the scheduled task, is that if there is some potential risk involved with running the service such as a memory leak or hanging network connection, then the windows service can potentially hang aroung for a long time, adversely affecting other users. On the other hand, the scheduled task is written to be short running, so even if it does leak, the effect is minimised.

On the other hand, someone in one of the above questions commented that the scheduler has a limit of accuracy of somewhere in the range of 1 minute, so you may see that the scheduler is unable to run your task every 30 seconds with accuracy.

Obviously there are a number of tradeoffs to consider, but hopefully this will help you make a good decision.

1800 INFORMATION
You could always meet in the middle, and have your process run every minute via scheduler, do the task, sleep 30 seconds, and then do the task again and quit.
ryeguy
Yeah, I would actually have the app run for 5 minutes (do 10 cycles with 30 seconds in between each). It is not critical if it goes longer between running instances... as long as it doesn't run MORE than once every 30 seconds.
Jason
A: 

30 seconds is a pretty short interval (relatively speaking) between processing cycles. Like the others I have my concerns about the task scheduler and I am afraid such a short interval will only compound the issues you might encounter if you took that approach. If this were my project I would almost certainly go with the service.

Brian Gideon