How can I get my windows service to run at a specific interval?

12,267

Solution 1

Well I have seen another question which discusses windows service vs scheduled task also this. So you might want to have a look at that other than that here is 2 similar questions which might help:

here too are some great links:

Solution 2

Take a look at Quartz.NET or search for "Windows Task Scheduling".

The first approach is programmatic and the later is configuration.

Solution 3

If you need to do an operation on a timer, then you have two choices

  1. Create a Windows Service and implement some kind of scheduling. This can be complex, so be prepared to that you may spend a lot of time solving issues that have already been solved.
  2. You could setup a scheduled task instead that calls your application.

It all depends how much control you really need.

Solution 4

You might want to use something like Quartz.net in your service. You can then use cron expressions to schedule "jobs" within your service.

Solution 5

Why not just add a timer to your service who does what you want on the interval you configure?

Share:
12,267
Ersin Gulbahar
Author by

Ersin Gulbahar

Technologies : Big Data, Informatica, Apache Hadoop, Apache Kafka, Confluent, Elasticsearch, Apache Sqoop, NoSql, Openshift Programming Languages : PYTHON, JAVA, T-SQL, PL-SQL, C#, Unity Databases : Oracle, MsSQL, Couchbase, HBASE, PostgreSQL, Vertica, Terradata, SAP Hana, MySQL Stackoverflow : https://stackoverflow.com/users/1332870/ersin-g%c3%bclbahar Dzone : https://dzone.com/users/4487476/ersingulbahar.html Github : https://github.com/ersingulbahar

Updated on June 25, 2022

Comments

  • Ersin Gulbahar
    Ersin Gulbahar almost 2 years

    I want to write a windows service. But it needs to run at specific times. For example, my service should email me every 5 minutes.

    private Timer _timer;
    private DateTime _lastRun = DateTime.Now;
    
    protected override void OnStart(string[] args)
    {
        _timer = new Timer(10 * 60 * 1000); // every 10 minutes
        _timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
        //...
    }
    
    
    private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        // ignore the time, just compare the date
        if (_lastRun.Date < DateTime.Now.Date)
        {
            // stop the timer while we are running the cleanup task
            _timer.Stop();
            //
            // do cleanup stuff
            //
            _lastRun = DateTime.Now;
            _timer.Start();
        }
    }
    

    I want my service to email me every 5 minutes.