Totallama Totallama - 1 month ago 8
C# Question

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?

geo geo
Answer

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);
            }   
        }
    }