2019-06-15 20:58:30

Hello, as the subject says, how to do something each hour? I mean, let's assume I launch the program at 1:35:13 PM. The program waits silently, and directly at 2PM does something. How should I achieve such thing?

If you want to contact me, do not use the forum PM. I respond once a year or two, when I need to write a PM myself. I apologize for the inconvenience.
Telegram: Nuno69a
E-Mail: nuno69a (at) gmail (dot) com

2019-06-16 17:18:13 (edited by Rastislav Kish 2019-06-16 17:22:56)

Hi,
you might be interested in:
https://docs.microsoft.com/en-us/dotnet … mework-4.8

Usage is simple:

using System;
using System.Timers;

namespace TimerExample {
class Program {
static Timer mainTimer;

static void Main(string[] args)
{
mainTimer=new Timer(1000); //Set up timer to fire each 1000 milliseconds
mainTimer.Elapsed+=OnMainTimer; //Set method receiving the event
mainTimer.AutoReset=true; //Timer don't fire just once, but will fire repeatedly in gived interval
mainTimer.Enabled=true; //Launch the timer

Console.ReadKey();
mainTimer.Stop(); //Stop the timer
mainTimer.Dispose(); //Dispose it
}

private static void OnMainTimer(object sender, ElapsedEventArgs e)
{
Console.WriteLine("One second passed.");
}
}
}

You can use this principle for example to check every minute, if requested time was reached and react. Just keep in mind, that excepting System.Timers.Timer, there is also System.Threading.Timer, so compiler could give you warnings, if you use the Threading namespace.

Best regards

Rastislav