Windows Service run every hour in specific minute

Marko

I need help. I have Windows Service and I need run this service every hour in specific minute for example: 09:05, 10:05, 11:05,.... My service now start every hour but every hour from time when i start this service. So how can I achieve my needs.

My code:

public partial class Service1 : ServiceBase
{
    System.Timers.Timer timer = new System.Timers.Timer();

    public Service1()
    {
        InitializeComponent();
    }

    protected override void OnStart(string[] args)
    {
        this.WriteToFile("Starting Service {0}");

        timer.Elapsed += new ElapsedEventHandler(OnElapsedTime);

        timer.Interval = 60000;

        timer.Enabled = true;
    }

    protected override void OnStop()
    {
        timer.Enabled = false;

        this.WriteToFile("Stopping Service {0}");
    }

    private void OnElapsedTime(object source, ElapsedEventArgs e)
    {
        this.WriteToFile(" interval start {0}");
    } }
Ivan Kishchenko

You should check current time every 'n' seconds (1 as example) from timer:

public partial class Service1 : ServiceBase
{
    System.Timers.Timer timer = new System.Timers.Timer();

    public Service1()
    {
        InitializeComponent();
    }

    protected override void OnStart(string[] args)
    {
        this.WriteToFile("Starting Service {0}");

        timer.Elapsed += new ElapsedEventHandler(OnElapsedTime);

        timer.Interval = 1000; // 1000 ms => 1 second

        timer.Enabled = true;
    }

    protected override void OnStop()
    {
        timer.Enabled = false;

        this.WriteToFile("Stopping Service {0}");
    }

    private int lastHour = -1;
    private void OnElapsedTime(object source, ElapsedEventArgs e)
    {
        var curTime = DateTime.Now; // Get current time
        if (lastHour != curTime.Hour && curTime.Minute == 5) // If now 5 min of any hour
        {
            lastHour = curTime.Hour;

            // Some action
            this.WriteToFile(" interval start {0}");
        }
    } 
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

run cron every minute during a specific hour?

How to run a cron jobs in every minute for a specific hour on server

How do I run a windows service in every one hour?

Why does my cron job run every 1 minute although I set it every 1 hour?

Why my cron job is running every minute when it is scheduled to run every hour?

Run code every hour of the hour

Run cron job every 2 minutes except the first minute "0" of the first hour "0"

Countdown timer every hour but on 30 minute marks

SQL get every hour and minute of day

Timer every hour but starting from 1 minute

Reload page every hour on the :00 minute and the :30 minute

Service restart every 2 minute

Run a function at the start of every minute?

Airflow: Run DAG every minute

Casting a specific hour and minute and getting the time difference

Crontab running python script every minute instead of every two hour

Run java function every hour

Run python script every hour

Maximize windows every Xth minute

Oozie - run a workflow every day or every hour

how to configure jenkins scheduler to run a job every hour at a specific time/min?

spring cron for running every hour at 35th minute

ElastAlert alert every hour instead of minute for a certain rule

React: how can I execute function at exact minute every hour?

My PC turns on by itself exactly at the 10 minute mark at every hour

Cron Job firing every minute for an hour at it's scheduled execution time

Android Service for uploading data for x minute in every x minute

JavaScript: get code to run every minute

How to run cron in serverless for every minute?