• 0

[C#] How do I pause/delay/wait in C#?


Question

Hi, I want to create an infinate loop that runs in the background. I guess I'll need to split it off into it's own thread.... but before I get that far, how do I put a delay in the loop, so that for example, it only goes through the loop every 5 seconds.

How?

Link to comment
Share on other sites

18 answers to this question

Recommended Posts

  • 0
Actually, the correct answer is by using a Timer control... just figured it out.

584745531[/snapback]

What? If it's on it's own thread then there's not problem with Thread.Sleep(). It's not going to tie up any resources. The timer's great though.

Link to comment
Share on other sites

  • 0

there's no "Correct" answer. you can do it both ways and both of them work great. the Thread.Sleep i personally thin is better cause you add even less things to the code.

Link to comment
Share on other sites

  • 0
there's no "Correct" answer. you can do it both ways and both of them work great. the Thread.Sleep i personally thin is better cause you add even less things to the code.

584749339[/snapback]

Agreed, that was the point I was trying to make. Guess it didn't sound like it. :whistle:

Link to comment
Share on other sites

  • 0
Agreed, that was the point I was trying to make.  Guess it didn't sound like it.  :whistle:

584749610[/snapback]

happens to everyone huh? :)

hope the kid solved his prob :p

Link to comment
Share on other sites

  • 0

yeah no correct answer here. That's the good thing about programming, many ways to do it! Just like cooking. :)

Link to comment
Share on other sites

  • 0
yeah no correct answer here. That's the good thing about programming, many ways to do it! Just like cooking. :)

584754287[/snapback]

And just like cooking, there are many ways to go horribly, horribly wrong :x.

I usually use the Timer function for when I want some code to execute a certain time after it is started. The Thread.Sleep is better for when you want to pause the execution.

Link to comment
Share on other sites

  • 0
And just like cooking, there are many ways to go horribly, horribly wrong :x.

I usually use the Timer function for when I want some code to execute a certain time after it is started. The Thread.Sleep is better for when you want to pause the execution.

584758671[/snapback]

Yeah, I ended up using the Timer function that fires an event every 1000 ticks. It was easier to implement than a thread for what I was trying to do.

Link to comment
Share on other sites

  • 0

What if I don't want the thread to "sleep"?

I just want it to complete the current function. Then, wait 5 seconds?

Thanks in advance.

you can do something like this:

for(int i = 0; i < 50; i++)

{

System.Threading.Thread.Sleep(100);

Application.DoEvents();

}

Link to comment
Share on other sites

  • 0

for(int i = 0; i &lt; 50; i++)
{
System.Threading.Thread.Sleep(100);
Application.DoEvents();
}

If you meant this in the UI-thread it's horribly wrong then - you should never sleep or block in the UI-thread.

Link to comment
Share on other sites

  • 0

What if I don't want the thread to "sleep"?

I just want it to complete the current function. Then, wait 5 seconds?

Thanks in advance.

Use a timer. As Andreas pointed out, sleeping/busy loop is going to block the UI thread, and that's not a good thing to do. Blocking means that the UI won't process any messages in its message loop for the time of the block. The user may try to drag or click or something and think the app is broken. Keep the UI thread free for UI messages.

Link to comment
Share on other sites

  • 0

for(int i = 0; i &lt; 50; i++)
{
System.Threading.Thread.Sleep(100);
Application.DoEvents();
}

If you meant this in the UI-thread it's horribly wrong then - you should never sleep or block in the UI-thread.

Yeah, but it's a lot lazier.

Link to comment
Share on other sites

  • 0
you can do something like this:

for(int i = 0; i < 50; i++)

{

System.Threading.Thread.Sleep(100);

Application.DoEvents();

}

In a Windows Form Application I created, I tried this, and other "Thread.Sleep()" statements and they didn't do anything - the app didn't "sleep" - though I invoked the System.Threading namespace and I even tried 15000 in there. I'm thinking the only way you can pause such an app is using a timer.tick function, and just doing a timer1.Start() and timer1.Stop() when you want it to go or stop - so maybe there is only one way to do it.

-Tom

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.