7

Possible Duplicates:
windows service vs scheduled task
Windows Service Vs Simple Program

I'm trying to create a program that runs periodically (say every 5 minutes), or have the program running and have it execute the function within it (every 5 minutes).

The program is to obtain data from a database when it's executed and then write this to (for now) say info.txt file (no sensitive stuff is contained in here). each time it writes to the file it should overwrite the existing info within the file.

The program should also be started automatically at windows start up. (thus no need to login on the machine and to execute the .exe [if it's a normal program and not a service])

In between the periods that it executes the program would have nothing to do.

Therefore, should I run this program as a Windows Service, or should I use the Task Scheduler to periodically start the program to do this? My goal is for this program to run as smooth as possible without clogging up resources. (eg. it shouldn't need more than 5% of the cpu)

I hope my question was clear enough.

Community
  • 1
  • 1
Raskaroth
  • 639
  • 1
  • 9
  • 25
  • What OS are you targeting? I doubt if the Task Scheduler can start programs without an interactive user logon. – Chris O Mar 30 '11 at 16:20
  • 1
    @Chris: you can tell task scheduler to start some some application under account you specify for that task – HABJAN Mar 30 '11 at 16:21
  • What are the targeted platforms? How long will the actual process take? Note that Task Scheduler will run every 5 minutes (however you can configure it to not run if another instance is already running.) In the Windows Service approach, you can "schedule" the 5 minute interval after the previous run has completed. – gooch Mar 30 '11 at 16:23
  • @HABJAN, you're correct, thanks for the info. I guess the question remains if Task Scheduler can run an app with the specified logon after windows starts up. – Chris O Mar 30 '11 at 16:26
  • There is another good thread on this topic here: http://stackoverflow.com/questions/5425292/windows-service-vs-simple-program/5425431#5425431 – Winger Mar 30 '11 at 16:23

3 Answers3

9

I would go with application that is triggered by task scheduler. Only thing that you need to worry about is to run single instance of your application.

You can set task to run under specific user account and to run even if user is not logged on. There are number of events that can trigger task star like "Windows start", "System Idle"...

Other advantage is: If something crashes you can set task scheduler to send you an email or alert you in number of ways. You can control "exit code" of your application and signal to task scheduler what's going on and what to do.

There are a lot of positive features that task scheduler offers but not many people are using them.

SwDevMan81
  • 48,814
  • 22
  • 151
  • 184
HABJAN
  • 9,212
  • 3
  • 35
  • 59
  • 1
    The biggest advantage of using the Task Scheduler is that the scheduling code is already written / tested. If you change your mind as to how you want to schedule it, no big deal - just change it in the configuration of the task. – RQDQ Mar 30 '11 at 16:45
  • @RQDQ: Yep, that's the point! – HABJAN Mar 30 '11 at 16:49
  • +1 for task scheduler. You're probably adding more complexity to a simple app than you need by making it a service. – Tim Scarborough Mar 30 '11 at 17:04
0

I would suggest a Windows Service for this. Might be a good idea to create both and compare what the resource usage is like anyway?

jmccarthy
  • 470
  • 4
  • 16
0

I would actually recommend going for both depending on the requirements of the task you wish to run. I generally build most functionality for scheduled services into a single class library and then wrap it in a console application to start with and for debugging. When satisfied I wrap it in a windows service and forget about it.

Considerations of using a console app:

  • Make sure you run it under system account if possible or you can put in a specific login under Run as in scheduler. This will ensure an interactive login is not required.
  • If having 2 instances of it running at the same time is a problem, make sure you name it clearly and check for an instance of it running in your main method and exit if it is. A windows service will avoid this issue.

Considerations of using a window service

  • Make sure you're educated on thread usage. Windows services will use less resources if managed properly, but can be tricky if it's new to you and end up leaking memory in timer based tasks.

..there's a lot more to consider, but code it right and you can start with one and move to the 2nd when you're confident about it.

Gats
  • 3,452
  • 19
  • 20