• 0

[C#] Pause without sleep()?


Question

9 answers to this question

Recommended Posts

  • 0
  linked said:
Short but sweet, I'm looking for a way to delay action in my programs without using a Thread.Sleep() or while(time!=this), or anything (as all of those hang my program while they're pausing).

There's got to be a better way! I've looked into threading, but I think I'm doing something wrong.

586148051[/snapback]

The only way I can do that is to create a event, so when this happens do this... :)

  • 0

Like I said, when I thread.sleep(), my whole application stops responding entirely. I'm not just waiting for milliseconds here, but one or two seconds at a time, and it renders my program unusable.

(I'm using thread.sleep() to space out SendKeys.SendWait()s, and there are two issues- First, new events are no longer responded to, because the thread is too busy sleeping and sending, and second, if I close the application early, it continues sending keys to other applications for about 5 seconds- Thread.sleep is causing troubles for me.)

EDIT- Chillbo- How would I create another thread for it? Like I said, I've tried, but I think I'm doing something wrong.

  • 0

It's very simple to create another thread:

you can do it like so:

private void Form1_Load(object sender, System.EventArgs e) {
	worker_thread = new Thread(new ThreadStart(thread_function));
	worker_thread.Name = "worker thread";
	worker_thread.Start();
}

private void thread_function(){
	while (true){
  Thread.Sleep(300);
  Console.WriteLine("{0} says hello",Thread.CurrentThread.Name);
	}
}

This will create another thread in the Form_Load event, and the other thread will repeatedly write it's name and hello.

As you can see, it's very simple.

And you'll have to stop the thread when you exit your application (the form in my example):

private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e) {
	if ( worker_thread != null  &&  worker_thread.IsAlive ) { 
  worker_thread.Abort();
  Application.DoEvents();
	}
}

(have no idea whys is this DoEvents needed, so it in some ms sample)

and if you want to update the gui from tihs worker thread, u'll have to create some deligates.

i'm not the best programmer here, but i hope it helps.

robotnic :)

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

    • No registered users viewing this page.
  • Posts

    • Microsoft improves Windows 11 Search UI settings, confirms group policy bug in build 27919 by Sayan Sen Microsoft has released a new Windows 11 build for Insiders flighting the Canary channel. The new build, 27919, brings a new refreshed UI/UX for the Search page in the Settings app. Alongside that, the company also fixes multiple bugs including one wherein File Explorer would crash if a user accessed the digital signature information via the right-click context menu. The build also has plenty of known issues too including one where multiple errors would pop up when working with the Group Policy editor, as well as a multi-monitor issue in remote desktop applications. The full changelog is given below: [Windows Search] Today, we rationalize and organize Windows Search settings under Settings > Privacy & security under a “Search permissions” settings page and a “Searching Windows” settings page. We are beginning to roll out a change that brings those two settings pages together so you can easily access to all the Windows Search settings under a single settings page via Settings > Privacy & security > Search. The page is refreshed with a new modern visual for you to clearly browse the settings and control your experiences inside Windows Search, with the items listed in a better order. The updated single settings page for Windows Search. The bug fixes are as follows: [File Explorer] Fixed an issue where File Explorer might crash when trying to view the digital signatures tab in the properties for a file. [Input] Fixed an issue which was causing the Microsoft Changjie Input method to not work properly in the previous build. Fixed an issue which was causing phonetic keyboards, such as the Hindi Phonetic Input keyboard and the Marathi Phonetic keyboard, to not work properly in the previous build. Finally, the known issues are: [General] [IMPORTANT NOTE FOR COPILOT+ PCs] If you are joining the Canary Channel on a new Copilot+ PC from the Dev Channel, Release Preview Channel or retail, you will lose Windows Hello pin and biometrics to sign into your PC with error 0xd0000225 and error message “Something went wrong, and your PIN isn’t available”. You should be able to re-create your PIN by clicking “Set up my PIN”. The progress wheel may show as a rectangle glyph instead of rendering correctly when upgrading to this build. [NEW] You may see multiple error pop ups about unexpected elements when opening Group Policy Editor. [NEW] There’s an underlying issue with dao360.dll in this build which may result in some apps crashing. [Remote desktop] [NEW] There’s an issue starting with the last build where remote desktop only uses your primary monitor even if it’s configured to use multiple monitors. You can find the official blog post here on Microsoft's website.
    • Subscribers who pay for Higher Tier pricing (and wealthy power-hungry wh0res in general) don't GAF about the value or having a conscience for their actions/decisions/impacts. They want POWER and authority to do WTF they wanna do, no matter how petty, ludicrous, wasteful, damaging, or useless. Sadly, there are many power-hungry folks out there than truly admit it! They truly believe that their banal requests for Wikipedia regurgitate is truly an academically deep thought experiment warranting any of their oft-untapped Thinking Model usage tokens. As the saying goes, Ya Can't Change Stupid (no matter how wealthy they are or appear to be).
  • Recent Achievements

    • One Month Later
      Jaclidio hoy earned a badge
      One Month Later
    • Week One Done
      Yawdee earned a badge
      Week One Done
    • Week One Done
      eugwalker earned a badge
      Week One Done
    • First Post
      Ben Gross earned a badge
      First Post
    • One Month Later
      chiptuning earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      640
    2. 2
      +FloatingFatMan
      174
    3. 3
      ATLien_0
      141
    4. 4
      Xenon
      118
    5. 5
      wakjak
      108
  • Tell a friend

    Love Neowin? Tell a friend!