I am working in a windows service which downloads data from some devices.How can I schedule my windows service to run in every ten minutes pragmatically.I wanted to download data from these devices in every 10 minutes.Is it a good method to use timer and put the code inside the Elapsed method? Or is there any other method to perform this task?
Asked
Active
Viewed 7,302 times
3
-
Use Windows' Task Scheduler. And please put a little more effort in your question, don't just copy the title and show what you have tried. Are you sure you want your _service to run_ every ten minutes, or do you want it to perform work every ten minutes? – CodeCaster Sep 15 '14 at 10:05
-
I wanted to perform the work every ten minutes. – Don Sep 15 '14 at 10:21
-
I think WTS is too heavyweight for this frequency - I'd go for a Windows Service with a timer. – PhillipH Sep 15 '14 at 10:36
2 Answers
3
Not sure how far you are in the process, but assuming you're just starting, follow the step-by-step instructions here to create a basic Windows service that can be installed using the InstallUtil.exe. If you would prefer to have the Windows service executable install and uninstall itself, follow the step-by-step instructions here. This should get you started.
Now, to have something run every 10 minutes, you'll need a timer. Here is an example of what that might look like.
using System.Timers;
public partial class Service1 : ServiceBase
{
private Timer _timer;
protected override void OnStart(string[] args)
{
_timer = new Timer(10 * 60 * 1000); // 10 minutes expressed as milliseconds
_timer.Elapsed += new ElapsedEventHandler(OnTimerElapsed);
_timer.AutoReset = true;
_timer.Start();
}
protected override void OnStop()
{
_timer.Stop();
_timer.Dispose();
}
private void OnTimerElapsed(object sender, ElapsedEventArgs e)
{
// Do your work here...
}
HTH

Community
- 1
- 1

Matt Davis
- 45,297
- 16
- 93
- 124
-
You might want to pause/stop the timer when entering the OnTimerElapsed method, and resume it before exiting the method if there is a possibility that your processing could run for more than 10 minutes. – Nattrass Oct 12 '14 at 15:52
2
You might want to pause/stop the timer when entering the OnTimerElapsed method, and resume it before exiting the method if there is a possibility that your processing could run for more than 10 minutes.

Anatoliy Nikolaev
- 22,370
- 15
- 69
- 68