tags:

views:

701

answers:

6

Could you please tell me how do I go about pausing my program for 500 milliseconds and then continue?

I read Thread.Sleep(500) is not good as it holds up the GUI thread.

Using a timer it fires a callback ...

I just want to wait 500ms and then continue to the next statement.

Please advise.

EDIT: I need to display a status bar message for 500ms and then update the message with a different one. Sorry, I meant 500 not 50.

EDIT: I do understand what all you have said. but: [I just want to wait 500ms and then continue to the next statement.] I think because it is such a short interval i am going do a Thread.Sleep(500) on the main GUI thread. Otherwise i would have to rewrite a lot of code to accomodate this brief interval of 500 milliseconds.

EDIT: i will try to reformat my status message so the pause is not needed.

+4  A: 

Instead of pausing the UI directly for 500 ms, you can always use a BackgroundWorker. That will cause your callback to run in a separate thread, where you can use Thread.Sleep to pause it without blocking the UI. Then when you are done, just update the status bar with your new message.

Justin Ethier
+2  A: 

More context to the question would be helpful.

Thread.Sleep(50) will pause the current thread for 50 milliseconds. If you're doing this in the UI thread, then yes, it will freeze the UI for 50 milliseconds. However, if you use a different thread to do this processing, then calling Sleep on that thread will pause it for 50 milliseconds without freezing your UI thread.

See Marc's answer to this question for an example on using a BackgroundWorker instance to do what you need.

Aaron Daniels
+5  A: 

You have two choices:

  • Use a timer as you suggested. Split your method up into two methods, foo1 and foo2. Use the foo1 to start the timer and run foo2 in the callback.
  • Use a BackgroundWorker for running the entire function and use Thread.Sleep on the worker thread.

From your update it seems that the only thing you want to do is change a single field. I would definitely recommend the first method: using a timer. Starting a BackgroundWorker for this task is overkill and will just give you unnecessary extra work and complications.

Mark Byers
the thing is i dont want a callback. i just want to continue to the next line after the pause.
iEisenhower
@ikurtz: Why don't you want a callback? You can put your callback inlined in your first function if your reason for avoiding callbacks is that you want to keep things together. But it sounds to me that the only answer you will be happy with is if someone tells you to use `Application.DoEvents()`. It will probably work but this is not the best way of doing it.
Mark Byers
A: 

You need to allocate another thread. In that thread you Sleep(500) and change the needed data. Caution: you would need to use the original thread's dispatcher, because the data related to UI should be usually updated from the GUI thread.

Vlad
+2  A: 

In C# your best bet is to use the Timer and fire a callback.

In F# there is an awesome way to do what you want, see

F# async on the client side

which shows how to write straight-line code and have the language take care of the callbacks for you.

Brian
+2  A: 

Hmya, what you're trying to do is pretty fundamentally incompatible with the Windows programming model. A native Windows program is event driven. Your program is always idle, sitting inside a loop started by Application.Run(), waiting for Windows to tell it that something interesting happened that it should respond to. Paint requests, mouse clicks, timer expirations, stuff like that.

Your program should respond to this and filter what is interesting to you. When you drop a button on a form, you are always interested in the Click event, generated when Windows sends the MouseDown notification message. Your Click event handler runs some kind of custom code that you write. Like updating a status bar message in your case.

Updating the status bar message half a second later doesn't make a whole heckofalot of sense. What exactly happened during those 500 milliseconds that changed the way your program responds to events? You can call the Update() method of the StatusBar so the new message is visible, then call System.Threading.Thread.Sleep(500) to get what you want. You'll get away with it, the "Not Responding" ghost that Windows puts up takes your program going dead for several seconds.

But that doesn't make a lot of sense, nothing happened during that half second, the state of your program didn't change. It couldn't change, it was dead to Windows and not receiving any messages that would allow it to change state.

Well, that's about as far as I can take this. Please update your question and explain why you need to do this. Just in case: if you're contemplating this to fake doing something important for half a second, your user will not be impressed. She'll eventually notice your UI is dead for half a second without anything to show for it.

Hans Passant
i am trying to present the application state info to the user in chunks. i have the complete info but i think it would be better if it was presented in two steps instead on a whole bunch of info all at once. [Your comments have been helpful, thank you].
iEisenhower
@ikurtz: Does that mean you want to inform your user about important steps you took in your code? That only took a few milliseconds, too fast for the user to see? No, she's not interested, she cares about the result. You already did the Right Thing, you made sure it was done *really* fast. Good job, your fellow programmers will appreciate it. Not your user. Well, she does, she expects it.
Hans Passant
Thank you. Again I liked your response as it made me review my actions and goals. At times it is better to offer thoughts than code solutions.
iEisenhower
@ikurtz: way to go. Thank you for the answer mark. Looking for a job around Madison by chance?
Hans Passant
Madison? Wisconsin? It is nice there but I am in England!
iEisenhower
Rats. Well, worth a shot. hpassant at google's email, if for some really mysterious reason you like crappy beer.
Hans Passant
@nobugs: google's email - got it. thanks.
iEisenhower