• 0

[C#] Pause without sleep()?


Question

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.

Link to comment
Share on other sites

9 answers to this question

Recommended Posts

  • 0
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... :)

Link to comment
Share on other sites

  • 0

I think sleep is the best option. Can't you just create another thread for it? Therefore e.g. GUI will run independently of 'sleeping thread'.

Chillbo

Link to comment
Share on other sites

  • 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.

Link to comment
Share on other sites

  • 0
thread.sleep is the better why. Why not use that?

586148186[/snapback]

But his doing just one thread, so if he uses that its going to make his program hang...

Link to comment
Share on other sites

  • 0

linked, Sorry C# is not my strongpoint, maybe this link will help. I'm sure you'll get it - otherwise there are plenty of people who will help out here...give it some time...and a bump or two!

Chillbo

Link to comment
Share on other sites

  • 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 :)

Link to comment
Share on other sites

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

    • No registered users viewing this page.