C# Pause Program Execution
Solution 1
Timer
's will fire off the Elapsed
event once the specified interval has elapsed.
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 could also implement this as a service so you don't have to have a blocking call like Console.ReadKey
to keep the program from terminating.
Finally, you could just change the interval in the event handler:
static void DoSomething(...)
{
timer.Stop();
timer.Interval = TimerMilliseconds();
...
timer.Start();
}
Solution 2
The problem with this code is that you're using a loop to set the Interval
and Enabled
properties of the Timer
, which will execute said assignments over and over - it's not waiting for the timer to execute in some way.
If your application doesn't need to be mutlithreaded, then you might be better simply calling Thread.Sleep
between executions.
class Program {
static void Main(string[] args) {
while(true) {
Thread.sleep(TimerMilliseconds()); // The duration of the wait will differ each time
DoSomething();
}
}
}
Solution 3
take out the timer and loop from your logic. Just use windows scheduler to execute your program after 15 minutes. Or you can use windows services. Please read Best Timer for using in a Windows service
Solution 4
remove the while loop completely.
inside of the DoSomething() function (once implemented) stop timer at start and at the end reset the interval before restarting the timer.
Related videos on Youtube
Comments
-
Joe about 1 year
I am writing a program that will perform an operation every 10 or 15 minutes. I want it to be running all the time, so I need something that is cheap on processing power. What I have read so far seems to suggest that I want to use a Timer. Here is a clip of the code I have so far.
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 here is that the while loop just keeps executing rapidly. How do I halt execution until the timer is elapsed. My program really doesn't need to be multi threaded. Is a Timer the right tool for this job?
Thank you in advance for any help!
UPDATE: Sorry for the confusion. I have implemented the DoSomething method. I just did not include it as I don't believe it is part of my issue.
-
Vlad over 8 yearsHint: Remove your while loop and implement
DoSomething
. -
Sam I am says Reinstate Monica over 8 yearsadditionally, you might also consider making your application into a Windows Service
-
-
Joe over 8 yearsThis is what I want, but I that using Thread.sleep was a no-no.
-
Joe over 8 yearsThis looks like what I want. I am using System.Timers.Timer. Do I need to manually stop the timer? I believe it stops automatically if AutoReset parameter is not set?
-
BradleyDotNET over 8 yearsThread.Sleep here is probably ok, but a Timer is still better
-
BradleyDotNET over 8 years@Joe You don't have to stop it, but if the process is long running, and the timer interval short, you could have two executing at the same time. Its up to you, and you could probably get away without it.