• 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

    • FIFA cup is a worldwide event. Total global engagement — FIFA World Cup Qatar 2022 (official FIFA report) 5 billion people https://theworlddata.com/fifa-...-cup-viewership-statistics/ U.S., Canada, Japan drive vast World Series viewership for Games 1 and 2 In Japan despite a 9 a.m. local start time, Game One averaged 11.8 million viewers on NHK-G https://www.mlb.com/news/2025-...ching-large-global-audience There are also millions of annual viewers of the World Series in Latin America, especially Venezuela and the Domincan Republic due to the large number of players from those countries playing in the Major Leagues.
    • The original word arts were far more awesome! With their own preconfigured fill patterns, 3D layout, etc. I especially loved the ones circled below and still miss them from my primary school years: I frankly use them less these days 'cause the new one isn't as straightforward fine art as what we originally had. Same with the built-in picarts selection.
    • What didn't you understand about that was mainly referring to Google, Microsoft, etc. keeping your passwords. Password Management is a key service of Bitwarden and it's not going anywhere. In any case they do offer export to other Password management services, backup/download of passwords, and the already mentioned on-prem option. I don't agree with the OP to use the free option as it's better to be an actual customer IMO. They don't just don't delete accounts like the big tech companies with no recourse which was the main concern of this article. I was confused if the author was trying to sell this setup? It should be obvious to anyone reading this article this solution is overly complicated and overkill for most users.
    • I got this notification just now in Android: So I went in to disable the "Other" or "Marketing" notifications in Notifications management: But it came through the Now Playing? So if I disable that I no longer get what's Now Playing in Notifications? I'm a paying subscriber, not on the free plan... can they sink any lower?
    • Population especially in high density areas creates more heat and more humidity. This can be noticed in an indoor arena or concert room which heats up when the room or arena fills with people, without air conditoning to cool it down, Watering of lawns creates more humidity as the moisture from the watering rises into the atmosphere, creating a more humid condition. The again, depopulating an arena or room after an event will drop the temperature inside. Desert areas are less humid for a number of reasons, including a lower population density. Tel Aviv has horrible weather, unless you like it hot and humid. Summer days are regularly 90+ F with humidity well over 70%. It is probably not as bad as Mississippi but still it is bad enough.
  • Recent Achievements

    • 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
    • One Month Later
      Genuinetonerink- Dubai earned a badge
      One Month Later
    • Week One Done
      Genuinetonerink- Dubai earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      504
    2. 2
      +Edouard
      163
    3. 3
      PsYcHoKiLLa
      91
    4. 4
      Steven P.
      75
    5. 5
      Michael Scrip
      72
  • Tell a friend

    Love Neowin? Tell a friend!