Search code examples
c#timer

The simplest possible infinitely repeat


I need the simplest possible Timer to repeat my code infinitely every 5 seconds. No external classes or whatnot.

Just:

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Now the following code will be repeated over and over");
    
        //////////////// FOLLOWING CODE /////////////////
        /* the repeated code */
        //////////////// END OF FOLLOWING CODE /////////////////

    }
}

How can I do that?


Solution

  • Use while(true) with Thread.Sleep

        using System.Threading;
    
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("Now the following code will be repeated over and over");
    
                while(true)
                {
                    //////////////// FOLLOWING CODE /////////////////
                    /* the repeated code */
                    //////////////// END OF FOLLOWING CODE /////////////////
                  Thread.Sleep(5000);
                }   
            }
        }