C # Sleep for 500 milliseconds

Could you tell me how I can pause my program for 500 milliseconds and then continue?

I am reading Thread.Sleep(500) not very well, as it supports GUI thread.

Using a timer, it calls a callback ...

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

Please inform.

EDIT: I need to display the message in the status bar for 500 ms and then update the message to others. Sorry, I meant 500 not 50.

EDIT: I understand what you said. but: [I just want to wait 500 ms and then move on to the next statement.] I think because this is such a short space that I'm going to make Thread.Sleep (500) in the main GUI thread. Otherwise, I would have to rewrite a lot of code to accommodate this short interval of 500 milliseconds.

EDIT: I will try to reformat my status message so that a pause is not needed.

+7
c # sleep timer
source share
6 answers

Hmya, what you are trying to do is fundamentally incompatible with the Windows programming model. The native Windows program is event driven. Your program is always idle, sitting inside the loop launched by Application.Run (), waiting for Windows to report that something interesting has happened that it should respond to. Paint requests, mouse clicks, timer exposures, such things.

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

Updating the status message half a second later does not make the whole heckofalot point. What exactly happened during these 500 milliseconds that changed the way your program responds to events? You can call the Update () method on the StatusBar to make the new message visible, and then call System.Threading.Thread.Sleep (500) to get what you want. You will get rid of it, the "Unrequited" ghost that launches Windows causes your program to go out within a few seconds.

But this does not make much sense, nothing happened in this half second, the state of your program has not changed. He could not change, he was dead for Windows and did not receive any messages that would allow him to change state.

Well, this is as far as I can take it. Please update your question and explain why you need it. Just in case: if you contemplate this to fake something important in half a second, your user will not be impressed. She will eventually notice that your user interface is dead for half a second so as not to show anything.

+5
source share

You have two options:

  • Use the timer as you suggested. Divide your method into two methods: foo1 and foo2. Use foo1 to start the timer and start foo2 in the callback.
  • Use BackgroundWorker to run the entire function and use Thread.Sleep in the workflow.

From your update, it seems that the only thing you want to do is change one field. I would definitely recommend the first method: using a timer. Running BackgroundWorker for this task is redundant and just gives you extra work and complications.

+5
source share

Instead of pausing the interface directly for 500 ms, you can always use BackgroundWorker . This will make your callback work in a separate thread, where you can use Thread.Sleep to pause it without blocking the user interface. Then, when you're done, just update the status bar with your new message.

+4
source share

More context to the question would be helpful.

Thread.Sleep(50) pause the current thread for 50 milliseconds. If you do this in the user interface thread, then yes, it will freeze the user interface for 50 milliseconds. However, if you use a different thread to do this processing, a call to Sleep on that thread pauses it for 50 milliseconds without freezing your user interface thread.

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

+2
source share

In C #, it is best to use a timer and trigger a callback.

There is a great way in F # to do what you want, see

F # async on the client side

which shows how to write straightforward code and the language should take care of callbacks for you.

+2
source share

You need to allocate another thread. In this thread you are Sleep(500) and change the necessary data. Note: you will need to use the source stream manager because the data associated with the user interface should usually be updated from the GUI thread.

0
source share

All Articles