1

I need to create a while loop with 10 seconds of delay between each iteration:

while (true) { // operation // delay for 10 seconds } 
5
  • 3
    Did you consider a Timer instead? Commented Aug 1, 2019 at 20:30
  • @Steve Yes, I really tried using a timer, but I didn’t succeed. Can you explain how to use it, please. Commented Aug 1, 2019 at 20:33
  • 1
    if you're in a context that allows async add await Task.Delay(10000);. Commented Aug 1, 2019 at 20:35
  • @TobiasTengler Ty very much Commented Aug 1, 2019 at 20:37
  • Possible duplicate of How do I get my C# program to sleep for 50 msec? Commented Aug 1, 2019 at 20:41

2 Answers 2

7

You could use Task.Delay for this:

var timespan = TimeSpan.FromSeconds(10); await Task.Delay(timespan); // or Task.Delay(timespan).Wait(); 

I'm recommending this over Thread.Sleep, since Thread.Sleep blocks your entire Thread while waiting, whilst Task.Delay allows the Thread to deal with other work, while waiting.

Sign up to request clarification or add additional context in comments.

Comments

0

Just insert a sleep timer inside the while loop that sleeps for 10 seconds.

See this thread: How do I get my C# program to sleep for 50 msec?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.