Tom
Tom

Reputation: 4377

C# - Looping methods and putting delays inbetween them

I have several methods that are declared in the Main method.

Although how could I put a loop in, so in this case OutputChanges() would loop around to FileChanges(). And is it possible to put an break / interval of say 10 seconds inbetween the loop?

 static void Main(string[] args)
        {


            FileChanges();

            FolderChanges();

            OutputChanges();

        }

Upvotes: 0

Views: 4825

Answers (5)

Reed Copsey
Reed Copsey

Reputation: 564333

I would recommend reworking this to use a Timer instead. The timer can tick every 10 seconds, at which time you can do your operations.

Upvotes: 1

ecMode
ecMode

Reputation: 540

static void Main(string[] args)
{
    int counter=0;
    do{
        counter++;
        FileChanges();
        FolderChanges();
        OutputChanges();
        Thread.Sleep(10000);
    }while(counter<10)
}

Upvotes: 1

Jamie Dixon
Jamie Dixon

Reputation: 53991

You could do:

static void Main(string[] args)
{

  while(true)
  {
     FileChanges();
     FolderChanges();
     OutputChanges();
     Thread.Sleep(10000);
  }

}

Upvotes: 1

Justin Niessner
Justin Niessner

Reputation: 245399

You don't mention how many times you want to loop...so I'll go with the infinite loop (using Thread.Sleep() to halt execution for 10 seconds between iterations):

static void Main(string[] args)
{
    while(true)
    {
        FileChanges();
        FolderChanges();
        OutputChanges();

        Thread.Sleep(10000);
    }
}

Upvotes: 2

Ed Swangren
Ed Swangren

Reputation: 124632

System.Threading.Thread.Sleep

static void Main(string[] args)
{
    while( true )
    {
        FileChanges();
        FolderChanges();
        OutputChanges();
        Thread.Sleep( 10000 );
    }
}

Upvotes: 2

Related Questions