Executing a function at a specific time

My application has a timer that I want to stop and start according to local time. So I need something like this:

if ( time = 08:00) {StartTimer();} If ( time = 18:00) {StopTimer();} //This can be done from the timer event itself 

Is there any way to do this without using another timer? I can stop the timer from the timer event itself, but how did I start it again?

+4
source share
4 answers

You can set the timer interval to 14 hours, and not stop it or maintain it with a short interval and check the additional condition (time of day) inside.

+2
source

You can try the following: -

1) Create a console application that does what you are looking for.

2) Use the Windows Scheduled Tasks function to start the console application at the time you need to run it

or

You can also look at this example: -

  using System; using System.Threading; public class TimerExample { // The method that is executed when the timer expires. Displays // a message to the console. private static void TimerHandler(object state) { Console.WriteLine("{0} : {1}", DateTime.Now.ToString("HH:mm:ss.ffff"), state); } public static void Main() { // Create a new TimerCallback delegate instance that // references the static TimerHandler method. TimerHandler // will be called when the timer expires. TimerCallback handler = new TimerCallback(TimerHandler); // Create the state object that is passed to the TimerHandler // method when it is triggered. In this case a message to display. string state = "Timer expired."; Console.WriteLine("{0} : Creating Timer.", DateTime.Now.ToString("HH:mm:ss.ffff")); // Create a Timer that fires first after 2 seconds and then every // second. using (Timer timer = new Timer(handler, state, 2000, 1000)) { int period; // Read the new timer interval from the console until the // user enters 0 (zero). Invalid values use a default value // of 0, which will stop the example. do { try { period = Int32.Parse(Console.ReadLine()); } catch { period = 0; } // Change the timer to fire using the new interval starting // immediately. if (period > 0) timer.Change(0, period); } while (period > 0); } // Wait to continue. Console.WriteLine("Main method complete. Press Enter."); Console.ReadLine(); } } 
+1
source

You can create a thread that ticks every second.

There you can check whether you want to start or stop your timer.

Read the following: Topics .

In your topic add something like:

 if (CurrentTime == "08:00") StartTimer(); else if if (CurrentTime == "18:00") StopTimer(); Thread.Sleep(1000); // Makes the Thread Sleep 1 Second 
+1
source

Since you need at least one timer that always works (to determine when it is 8am), then you can just have only one timer that runs all day.

Whenever the timer goes off, check the time. If it is not between 0800 and 1800, just return without any action and wait for the next tick.

You can try to increase the timer interval to a value that will lead you to an example. 17:55, and then reduce it again, but there will be no measurable difference in performance, so IMHO this will not be useful.

+1
source

Source: https://habr.com/ru/post/1441752/


All Articles