How can I schedule my Windows service to start every 10 minutes

I am working in a windows service that downloads data from some devices. How can I schedule my windows service to start every ten minutes pragmatically. I wanted to download data from these devices every 10 minutes. Is it a good technique to use a timer and put the code inside the Elapsed method? Or is there any other way to accomplish this task?

+3


source to share


2 answers


Not sure how far you are in the process, but if you are just getting started, follow the step by step instructions here to create a basic Windows service that can be installed using InstallUtil.exe. If you would prefer a Windows executable to install and uninstall, follow the step-by-step instructions here . This should get you started.

Now, for something to run every 10 minutes, you need a timer. Here's an example of what this 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...
    }

      

NTN

+3


source


You can pause / stop the timer when you enter the OnTimerElapsed method and resume it before the method exits if there is a chance your processing could run for more than 10 minutes.



+2


source







All Articles