views:

1452

answers:

4

I want to write a windows service which the user can schedule. i.e, the user can choose to run the service from 9:00 AM to 6 PM daily, or he could run it every night, starting from night 12 o clock at night to next day morning 6, etc. Is there any out of the box .NET API that will help me do this? I know I can do this using the Scheduled tasks, but is there any way to do this programmatically?

A: 

If you're going to schedule it, just build a console program and add some code to the installer that helps the user setup a scheduled task in windows.

Joel Coehoorn
+6  A: 

My first response is to question why a service? But more importantly, the question would be why not use the powerful scheduler that is provided by the operating system?

That said, a windows service is pretty much just a thread that your application runs in. You could ship it in two parts, the first is the service itself which executes on a timer. The startup of the service could check a registry value to determine how often it's supposed to execute.

The second part of the service would be a little windows app that allowed the user to set the schedule, and, of course, write it to the previously mentioned registry value.

There isn't any sort of special API that you'd need.

Chris Lively
+6  A: 

I've had good results using Quartz.NET to perform scheduled tasks inside a Windows service. You can do everything from simple interval scheduling, to cron-style schedules.

Joel Mueller
+2  A: 

If you don't want the user to have to deal with the task scheduler, then you should write a program that will let them pick the day and time to run the program, and then you programatically setup the scheduled task for them. That way they never have to know specifically about what process you are running, and they also don't have to know how to use task scheduler. They just do it all from your app.

Micah