• 0

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


Question

18 answers to this question

Recommended Posts

  • 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:

  • 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.

  • 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.

  • 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();

}

  • 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.

  • 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

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

    • No registered users viewing this page.
  • Posts

    • Looks like the 7.1 is fake effects, can it at least do real 5.1? It says 'virtual 7.1' in all descriptions.
    • You can't, if you didn't notice, it doesn't support surround at all, it's right in the spec list.
    • Hi — I’m always interested in soundcards. Like displays, I just want to know I’m getting as much clean “sonic juice” into my brain as possible as the years take their toll. I’m not entirely sure what to take away from this review, though. It doesn’t really tell me whether the AE‑X is a good product or who it’s actually for. Most of what I’m getting is: there’s a driver to install, here’s what it looks like, and here’s what’s in the box. There’s a lot of emphasis on the SPDIF input. When you mention not needing to switch headphones between console and PC — does that mean the PC has to be powered on just to pass audio through? That seems like a fairly big waste of energy. Is this more something a streamer would use alongside a capture card? How are you testing the sound? (Also, you might want to clarify that you’re using the headphones in wired mode when you mention they’re wireless.) You mention the lack of EMI shielding — how much real‑world difference does that make compared with typical motherboard audio? On multi‑channel: what exactly isn’t supported? Does this mean Windows spatial audio (Dolby Atmos for Headphones, DTS Headphone:X, etc.) won’t work, or just that the card itself doesn’t decode surround formats? And are there any true multi‑driver “surround” headphones left that would even use that? You also highlight support for high‑impedance headphones — but what does that translate to in practice? How does it compare to driving the same headphones from a normal device, and does it make any difference for everyday, lower‑impedance models? In short, who is this card actually targeted at?
    • Yes, THIS is wordart, not the styling that can now be done. Wordart was all about those curvy words, that you could change the path of, like making words go around a circle. I don't think it can be done now, right?
  • Recent Achievements

    • Dedicated
      Almohandis earned a badge
      Dedicated
    • Dedicated
      JuvenileDelinquent earned a badge
      Dedicated
    • First Post
      DrWankel earned a badge
      First Post
    • Reacting Well
      DrWankel earned a badge
      Reacting Well
    • Week One Done
      Supreme Spray LV earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      503
    2. 2
      +Edouard
      170
    3. 3
      PsYcHoKiLLa
      88
    4. 4
      Steven P.
      76
    5. 5
      Michael Scrip
      74
  • Tell a friend

    Love Neowin? Tell a friend!