Reputation: 31
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?
Upvotes: 3
Views: 7922
Reputation:
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.
Upvotes: 2
Reputation: 46052
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
Upvotes: 4