• 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 seems backwards. You should have to explicitly authorize files for outside use. It shouldn't be the default.
    • Wow you are right, I never even noticed this until you said it! (870E Aorus Master) Before testing this card I had a TP-Link tx401 10GbE PCIe card in that slot (now using XikeStor 310 Thunderbolt to Ethernet adapter).
    • Lack of 5.1 makes this a no sale. What I really wish creative would make would be a USB version that supports 5.1 analog audio for speakers, can switch to headphone mode, and matches the specs of their top tier cards. The current X870E AMD motherboards dont have a great option for adding a PCIE x1 card without cutting down pcie lanes to the graphics card.
    • The Microsoft Office feature that time forgot by Usama Jawad I have been actively using Microsoft Word for the past couple of decades in academic, professional, and personal capacities. Although I used it through the perpetual version of Office apps at the start, I have been an active subscriber of Microsoft 365 Family subscription for over five years now. This means that my Word installation is regularly updated with new features, some of which I don't really like, but that's beside the point. As new features get continuously added, old ones that used to be a staple of Word have started to take a backseat. While I was reminiscing over my Windows experiences from my childhood today, I suddenly remember one such capability that I heavily used in my younger years but have not really touched in over a decade, and haven't seen in documents created by others either. That feature is WordArt. Just to clarify, WordArt is not a Microsoft product specific to Word, and is included in other Office apps like PowerPoint and Excel too. However, Microsoft Word is the app that I used Word Art in heavily, while making assignments or other deliverables for school. If you're unaware, WordArt offers a collection of styling techniques for text, introducing 3D effects like shadows and reflections in the text. It used to be one of the coolest things ever when I was at school and me and my classmates would often compete to ensure that our assignment's title in WordArt was truly the best and stood out above the rest. See some examples of WordArt, still present in the latest versions of Word below: Although WordArt is undeniably cool, it has taken a bit of a backseat, and has been relegated from the Home tab to the Insert toolbar, along with a bunch of other utilities, making it very easy to miss. This isn't exactly surprising because WordArt doesn't really have a place in academic and professional documents anymore, and while I have seen some creatives using it while developing promo material, even that sector has gravitated more towards dedicated graphic designing tools in the past years. For the vast majority of us, WordArt doesn't really exist, and that's alright. At least, it's still an option that can allow us to reminisce our childhood or even make some quick text stylization, if we really need it. Perhaps its usage has waned over time or the novelty has worn off, but I haven't even seen children use it in their academic assignments anymore. In fact, many don't even know that it exists. Maybe that's the fate of every beloved feature. What once felt cutting edge eventually becomes a relic, quietly tucked away as tastes, trends, and technology move on. WordArt may no longer dominate school assignments or decorate the covers of classroom projects, but for those of us who grew up with it, it remains a colorful reminder of a time when making text glow, bend, and cast giant shadows felt like the height of creativity. Hidden behind a few clicks in modern Word, it still survives not as a productivity tool, but as a small piece of computing nostalgia that refuses to disappear.
  • 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
      495
    2. 2
      +Edouard
      158
    3. 3
      PsYcHoKiLLa
      88
    4. 4
      Steven P.
      74
    5. 5
      Michael Scrip
      70
  • Tell a friend

    Love Neowin? Tell a friend!