Ethan Allen
Ethan Allen

Reputation: 14835

How do I do an infinite loop in C# with a 1 minute delay per iteration?

How do I execute an infinite loop in C# with a 1 minute delay per iteration?

Is there any way to do it without using some kind of variable with x++ and setting x to some incredibly large number?

Upvotes: 2

Views: 12541

Answers (5)

Sudhakar Tillapudi
Sudhakar Tillapudi

Reputation: 26209

Solution1 : If you want to wait for 1 minute without hanging your Main Thread, it is good to use Timer Control.

Step 1: You need to Subscribe to the Timer Tick event.
Step 2: Set the Interval property of the Timer to 60000 milliseconds for raising the event for every Minute.
Step 3: In Tick Event Handler just do ehatever you want to perform.
Step 4: you can Call the timer1.Stop() method whenever you want to stop the timer.

Note : if you don't stop the timer it becomes infinite. if you want to stop the timer you can call timer1.Stop();

    System.Windows.Forms.Timer timer1 = new System.Windows.Forms.Timer();
    timer1.Interval=60000;//one minute
    timer1.Tick += new System.EventHandler(timer1_Tick);
    timer1.Start();

    private void timer1_Tick(object sender, EventArgs e)
    {
         //do whatever you want 
    }

Solution 2:

EDIT : From the below comments : if the OP(Original Poster) is Trying to run this from Console Application System.Timers.Timer can be used

Note : instead of Handling Tick Event , OP has to handle the Elapsed Event.

Complete Code:

    class Program
    {
        static  System.Timers.Timer timer1 = new System.Timers.Timer();
        static void Main(string[] args)
        {
            timer1.Interval = 60000;//one minute
            timer1.Elapsed += new System.Timers.ElapsedEventHandler(timer1_Tick);
            timer1.Start();
            Console.WriteLine("Press \'q\' to quit the sample.");
            while (Console.Read() != 'q') ;
        }
        static private void timer1_Tick(object sender, System.Timers.ElapsedEventArgs e)
        {
            //do whatever you want 
            Console.WriteLine("I'm Inside Timer Elapsed Event Handler!");
        }
    }

Upvotes: 15

Tesserex
Tesserex

Reputation: 17314

while (true)
{
    System.Threading.Thread.Sleep(60000);
}

Now if we assume you don't want this thread to block and you're ok dealing with threading concerns, you can do something like this:

System.Threading.Tasks.Task.Run(() =>
{
    while (true)
    {
       // do your work here
        System.Threading.Thread.Sleep(60000);
    }
});

The Task will put your work on a ThreadPool thread, so it runs in the background.

You can also look at a BackgroundWorker if that's more geared toward what you want.

Upvotes: 4

crthompson
crthompson

Reputation: 15865

while(true){
    Sleep(60000);}

This would be a blocking call, so you would want to put it on its own thread or any kind of UI that you would have would hang badly.

Sleep is in the System.Threading.Thread namespace.

Upvotes: 0

Krease
Krease

Reputation: 16215

From a similar question on MSDN: >

System.Threading.Thread.Sleep(5000);

this codes make your application waiting for 5 seconds.

Change the number as necessary for the amount of time you want to sleep for (for one minute, this would be 60000). You can put this where you want in your while loop

Upvotes: 0

It'sNotALie.
It'sNotALie.

Reputation: 22794

for(;;)
{
    //do your work
    Thread.Sleep(60000);
}

This is not optimal but does exactly what it's asked.

Upvotes: 3

Related Questions