• 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

    • This Samsung T7 external SSD deal lasts less than a day by Sayan Sen Recently we had covered some nice deals of internal NVMe SSDs which include the 4TB TeamGroup G50 for only $400, the WD_BLACK SN7100 2TB for just $243, as well as the Samsung 990 PRO 1TB for $370. If however you require an external SSD for portability and quick data transfers and have a budget of less than $200 the Samsung T7 1TB model is currently on a limited time deal at just $190, it's lowest price in nearly three months. The deal ends today so you better hurry if you need one (purchase link below). The T7 weighs in at just 72 grams meaning it should be fairly easy to carry around helping in the portability department. Via its USB 3.2 Gen 2 interface the T7 promises sequential read speeds of up to 1050 MB/s and writes of 1000 MB/s. It is also fairly robust with a drop protection of up to 2 meters, though bear in mind that this is not waterproof. For that you will have to choose the rugged T7 Shield. The technical specifications of the Samsung T7 1TB are given in the table below: Specification Value Model Code (1TB) MU-PC1T0T / MU-PC1T0H Interface USB 3.2 Gen 2 (10 Gbps) Dimensions (W × H × D) 85 × 57 × 8 mm Weight 72 g Sequential Read Speed Up to 1,050 MB/s Sequential Write Speed Up to 1,000 MB/s Drop Resistance Up to 2 m (6.6 ft) Encryption AES 256-bit hardware encryption Operating Temperature 0°C to 60°C Non-Operating Temperature -40°C to 85°C Humidity 5% to 95% (non-condensing) Shock Resistance 1,500 G, duration 0.5 ms, 3-axis (non-operating) Vibration Resistance 20–2,000 Hz, 20 G (non-operating) Get it at the link below: Samsung T7 Portable SSD, 1TB External Solid State Drive, MU-PC1T0T/AM, Gray: $189.98 (Sold and Shipped by Amazon US) Good to know This Amazon deal is U.S. specific, and not available in other regions unless specified. We only use first-party seller links (at the time of article publishing); ensure that you purchase from a first-party seller link only. Check out Today's Deals on Amazon | or our recent tech deals. Become a Prime member (for Students or SNAP) via Neowin Get Prime Access - Prime for half price (for qualifying Medicaid, EBT, SNAP) Subscribe to Prime Video, Audible Plus, Music Unlimited or Kindle Unlimited via Neowin As an Amazon Associate, we earn from qualifying purchases.
    • I just wish they would put more love into Virtual Desktops. There is just so much more they could do.
    • Sounds like you just skimmed over the review, I answer all those questions in it. Although I admit I did not test if DTS works over the headphones, when I get some time I will test this. My personal use case is more speaker-driven (I cover this in the review too).
    • It actually does the same exact thing as yours, 7.1 virtual surround over headphones. From the product page: "Experience sound that surrounds you with 7.1 virtual surround, delivering a wider, more immersive soundstage for movies, games, and music." So both of them are two channel, with 7.1 virtual surround that only work in Headphones mode.
  • 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
      507
    2. 2
      +Edouard
      185
    3. 3
      PsYcHoKiLLa
      84
    4. 4
      Michael Scrip
      78
    5. 5
      Steven P.
      76
  • Tell a friend

    Love Neowin? Tell a friend!