C # Pausing program execution

I am writing a program that will perform an operation every 10 or 15 minutes. I want it to work all the time, so I need something cheap on computing power. What I read so far seems to suggest that I want to use a timer. Here is the clip of the code that I have.

class Program { private static Timer timer = new Timer(); static void Main(string[] args) { timer.Elapsed += new ElapsedEventHandler(DoSomething); while(true) { timer.Interval = TimerMilliseconds(); // The duration of the wait will differ each time timer.Enabled=true; } } } 

The problem is that the while loop just executes quickly. How to stop execution before the timer expires. My program really does not require multithreaded processing. Is Timer the right tool for this job?

Thank you in advance for your help!

UPDATE: Sorry for the confusion. I applied the DoSomething method. I just did not turn it on, because I do not think that this is part of my problem.

+5
source share
6 answers

Timer will disable the Elapsed event after a specified interval.

I would do something like this:

 private static Timer timer = new Timer(); static void Main(string[] args) { timer.Elapsed += new ElapsedEventHandler(DoSomething); timer.Interval = TimerMilliseconds(); // The duration of the wait will differ each time timer.Enabled=true; Console.ReadKey(); //Wait for keypress to terminate } 

You can also implement this as a service, so you do not need to have a blocking call like Console.ReadKey so that the program does not end.

Finally, you can simply change the interval in the event handler:

 static void DoSomething(...) { timer.Stop(); timer.Interval = TimerMilliseconds(); ... timer.Start(); } 
+4
source

The problem with this code is that you are using a loop to set the Interval and Enabled Timer properties that will perform the specified assignments over and over - this is not waiting for the timer to execute in some way.

If your application does not require mutlreadread, then it might be easier for you to simply call Thread.Sleep between execution.

 class Program { static void Main(string[] args) { while(true) { Thread.sleep(TimerMilliseconds()); // The duration of the wait will differ each time DoSomething(); } } } 
+2
source

completely remove the while loop.

inside the DoSomething () function (after implementation), stop the timer at startup and at the end reset the interval before restarting the timer.

+1
source

pull the timer and loop out of your logic. Just use the window scheduler to execute your program in 15 minutes. Or you can use windows services. Please read Best Timer for use in Windows service.

+1
source

I think the comments and annuses already provide the hints you need, but the MSDN docs for Timer do provide a good example. In my opinion, the Timer approach is a little more accurate, it’s easier for you to read your intentions and to ignore the details of calling your planned code.

0
source

Here is another alternative approach using ManualResetEvent and WaitOne (). This will allow you to stop the main thread without worrying about being accidentally killed by accidental key presses. You can also set () MRE when certain conditions are met so that the application can exit gracefully:

 class Program { private static Timer timer; private static ManualResetEvent mre = new ManualResetEvent(false); static void Main(string[] args) { timer = new Timer(TimerCallback, null, 0, (int)TimeSpan.FromMinutes(15).TotalMilliseconds); mre.WaitOne(); } private static void TimerCallback(object state) { // ... do something in here ... Console.WriteLine("Something done at " + DateTime.Now.ToString()); } } 
0
source

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


All Articles