C# Pause Program Execution

11,650

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.

Share:
11,650

Related videos on Youtube

Author by

Joe

Many years of Java experience.

Updated on June 09, 2022

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
      Vlad over 8 years
      Hint: Remove your while loop and implement DoSomething.
    • Sam I am says Reinstate Monica
      Sam I am says Reinstate Monica over 8 years
      additionally, you might also consider making your application into a Windows Service
  • Joe over 8 years
    This is what I want, but I that using Thread.sleep was a no-no.
  • Joe over 8 years
    This 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
    BradleyDotNET over 8 years
    Thread.Sleep here is probably ok, but a Timer is still better
  • BradleyDotNET
    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.

Related