Isaiah Nelson
Isaiah Nelson

Reputation: 2490

How to start and stop a windows service on a schedule?

I have a service that I have written to perform batch duties, but a requirement is to start the processing at 4:00pm and end its processing at 5am. If there is a job still processing, the time would need to be extended until the job is finished.

Would I wrap the onstart call in a timer or what? I really dont know what this would look like as I am used to services starting and running until infinity (or until the box tanks.. or an unhandled exception is met).

I would definitely appreciate some ideas on how to achieve this.

Edit: I do need a service because I am executing an SSIS package through dtexec() and a batch file in C#. Some other things have to happen as well like logging to a dbase and queue management.

Each job has a varying time length, some will complete in a few minutes, others make take 5 hours.

Upvotes: 2

Views: 7937

Answers (5)

CodingGorilla
CodingGorilla

Reputation: 19862

This sounds like a task that is better handled by the Windows Task Scheduler. Unless you're running something like Win2K or XP, it has all the settings necessary to start at a certain time, allow it to run for a limited amount of time, it can start the process under specific credentials (if you need it to run elevated), as well as run when no one is logged on.

And it has a nice handy GUI for managing it all, no coding necessary!

UPDATE

OK, based on your comments, what you really need is a workflow. What I would do, is develop each of the individual tasks in a seperate project (seperate .exe, or .dll depending on your needs). And then look and Windows Workflow Foundation and develop a workflow "controller" that you can use to run the individual tasks.

Upvotes: 4

Chase
Chase

Reputation: 564

Three options:

  1. You could use Windows task scheduler and start the task at the designated time and then keep track within the program how long the task has been running.

  2. Have the service constantly "running" but sleeping for a certain amount of time between executions. This would lead to problems if the program ran longer than anticipated, however.

  3. Set the service to run at 15 minutes (or so) intervals and check to see what time it is. If it is during your starting period, start the program.

The first and 3rd seem like the best options.

Upvotes: 1

Fen
Fen

Reputation: 943

You could have a look at quartz.net, it is a good scheduling library that does this sort of thing and can run in process to your service.

Upvotes: 1

deltree
deltree

Reputation: 3824

what you're looking for is a scheduled job

if you /must/ have a windows service, you use Timers and set the timer to check once every (whenever, I use 30 minutes for this sort of thing) and if the time is between 4am and 5am, do work.

Upvotes: 1

Igarioshka
Igarioshka

Reputation: 677

I think that the service should be ran until infinity and check the time to see if it's 4:00pm already and if so, start it's work, and when 5am strikes, just finish up and whait for 4:00 pm again.

Upvotes: 1

Related Questions