• 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

    • Isn't the CPU used to calculate the parity for the RAID? If so, the combination of SSDs and 10GBe might make the CPU more important
    • yeah GSMA began working to enable end to end encryption between android and iphone last year and apparently a new standard was developed. apple has said that they would implement this in "future software updates" but i haven't heard anything since march, the time this was all reported on. shortly after, i read on forbes that the FBI suggests not sending texts between iphone and android because they're unencrypted. i use signal to chat with my wife but i'd rather just use messages tbh (she has an iphone), i'm not really a 3rd party guy haha
    • Well, I did not like the trailer for the project he went to work on also, but why do you think he should waste time with this… did you love the season 2? Maybe I am missing out after the crap I saw in first season ep1-3? I love the first last of us game… while not the BEST it was one of the games that I will remember for the EXPERIENCE it game me… last of us 2 was not on the same level at all and the show🤔 complete miss in my experience of the first few level
    • They're likely moving all resources to other things. Clearly Windows is not important to them.
    • Image Uploader 1.4.3 Build 5352 by Razvan Serea Image Uploader is a free and open-source program for Windows that that allows you to effortlessly upload images, screenshots, and various files to a wide array of hosting services. With its capability to capture selected screen areas, it promptly uploads content to image hosting services, while also offering the convenience of automatically copying the URL to your clipboard. Key Features of Image Uploader: Upload to Multiple Hosting Services Image Uploader supports uploading images and files to over 30 popular hosting services. Additionally, it can upload directly to your own FTP, SFTP, or WebDAV server. After upload, the tool automatically generates sharing codes in HTML, BBCode, and Markdown, with support for custom output templates tailored to your needs. Video Frame Grabbing and Screenshot Tools You can extract multiple frames from video files in a wide range of formats including AVI, MP4, MKV, WMV, and more. It supports both system-installed codecs and built-in ones. The extracted frames can be uploaded individually or compiled into a single mosaic image. It also includes screenshot capabilities for the full screen or selected regions, along with a simple image editor for annotations, highlights, and blurring. Advanced Integration and Usability Image Uploader supports drag-and-drop, clipboard monitoring, and can be accessed via Windows Explorer’s context menu. It also features URL shortening, multi-account support, reuploading, and the ability to upload images embedded in text while retaining formatting. The app is available in several languages, including English, Russian, Turkish, Korean, Arabic, and more. Image Uploader 1.4.3 Build 5352 changelog: New Features Screen Recording: Added two powerful capture methods: DirectX (Desktop Duplication API) FFmpeg-based recording Expanded Hosting Services: Added support for new file hosting platforms: TeleBox (linkbox.to) take-me-to.space ranoz.gg webshare.cz lobfile.com imgpx.com freeimghost.net radikal.cloud anonpic.org fotozavr.ru imgtr.ee thumbsnap.com 8upload.com filemail.com Others Video Uploads: Added Flickr.com support for video uploads Localization: New French translation added Context Menu: Added "File Information" option to video file context menus DPI Support: Improved support for: Screen DPI changes Mixed-DPI multi-monitor setups Improvements Disabled application window animations during screenshot/screen recording initiation Updated API and documentation Improved overall stability Bug Fixes Fixed network client error that could cause application crashes Resolved unauthorized startup registration issue Fixed upload functionality for pixeldrain.com Restored tray icon balloon notifications visibility Various minor bug fixes Download: Image Uploader 64-bit | Portable 64-bit | ~16.0 MB | (Open Source) Download: Image Uploader 32-bit | Portable 32-bit | ~15.0 MB Download: Image Uploader ARM64 | Portable ARM64 | ~11.0 MB Links: Image Uploader Home Page | Screenshot | GitHub Get alerted to all of our Software updates on Twitter at @NeowinSoftware
  • Recent Achievements

    • Reacting Well
      SteveJaye earned a badge
      Reacting Well
    • One Month Later
      MadMung0 earned a badge
      One Month Later
    • One Month Later
      Uranus_enjoyer earned a badge
      One Month Later
    • Week One Done
      Philsl earned a badge
      Week One Done
    • Week One Done
      Jaclidio hoy earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      429
    2. 2
      ATLien_0
      156
    3. 3
      +FloatingFatMan
      149
    4. 4
      Nick H.
      64
    5. 5
      +thexfile
      62
  • Tell a friend

    Love Neowin? Tell a friend!