Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows Service that runs Periodically

Tags:

I'm writing a windows service that once started will run every X hours. The process it completes is fairly intensive, so I want to use a background worker. I'm using a Settings file to store both the hours between runs and the last time the service ran.

I'm not exactly sure the best way to do this - that is, I want the service to idle using as few resources as possible, and when it runs, it needs to run in the background worker, report what it did, and then go back into idle mode.

I've thought about using 2 background workers. The first worker would be a private local variable for the service that runs something like this:

while (true) {       //create new background worker and run        Thread.Sleep(Settings.Default.SleepTimeHours * 3600000); } 

with a sub worker that is created each iteration of the loop, and destroyed when completed. To support cancellation, I think I would have to have a local instance of the second worker available in the service, but it will be null if the process currently is not running. When the secondary worker completes, it would send out my reporting, set the last run time in the settings file, and then dispose of the worker and set the reference to null.

I'm wondering if there is a better way to do this or a best practice.

Thanks

like image 483
Josh Avatar asked Sep 30 '09 14:09

Josh


People also ask

How do I run a Windows service at a specific time?

windows service is useful when you want run task many time in minute. so if you want to run a program in a specific time, its better to use task scheduler and set event of task scheduler on a specific time in day.

Does Windows service run continuously?

Once the win service is started then this process will run continuously in background.

How do I schedule a Windows service to restart?

Click the Targets tab and select the Devices to have services restarted. Click the Schedule tab. From the Type drop-down menu, select Recurring. From the Interval drop-down menu, select Custom and define the weekly time frame.

How do you call a method periodically in C#?

1 Answer. Show activity on this post. Timer is the simplest and easiest way. There are at least a million ways of solving this, some are better for some scenarios - I assumed OP wanted to have the simplest and shortest one, and in my opinion that is the Timer.


2 Answers

I typically use a Timer, then stop it when the process starts to run.

Here's an article that explains how to do it.

like image 150
CitizenBane Avatar answered Sep 24 '22 17:09

CitizenBane


This is not a very good idea, since you lock your thread for the full period of "SleepTimeHours" and you won't be able to even stop the service in the meantime.

You could either make this service so that it would sleep for e.g. 5 seconds and then check whether it's time to get back to work, and if not, sleep another 5 seconds (that would give you the necessary responsiveness, if you need to stop the service).

OR: you might be better off just writing a console app that can be scheduled using the Windows "scheduled tasks" feature in order to be run every x hours. That way, you won't be blocking or using any system resource if your app isn't doing anything......

Marc

like image 43
marc_s Avatar answered Sep 21 '22 17:09

marc_s