|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
VB.net sleep problemsI wrote a small program for primary school children.
Now I want to add a pause before changing the color of a label control and I decided to use Sleep. I was having a lot of problems so I decided to simplify the code. Now I have a keypress event on a form with the following code: labNote.ForeColor = Color.Green System.Threading.Thread.Sleep(2000) labNote.ForeColor = Color.Red I dont' know why but the code doesn't work: I don't have the sequence green-sleep-red but i have green-red-sleep. Where is the problem? I'm putting to sleep the wrong thread? I didn't create any thread but... I really don't understand. "ringhio" <ziol***@gmail.com> schrieb labNote.Refresh> I wrote a small program for primary school children. > Now I want to add a pause before changing the color of a label > control and I decided to use Sleep. > I was having a lot of problems so I decided to simplify the code. > Now I have a keypress event on a form with the following code: > > labNote.ForeColor = Color.Green > System.Threading.Thread.Sleep(2000) If you don't add Refresh, the label won't update before the thread is idle.> labNote.ForeColor = Color.Red > > I dont' know why but the code doesn't work: I don't have the > sequence green-sleep-red but i have green-red-sleep. > > Where is the problem? I'm putting to sleep the wrong thread? I > didn't create any thread but... > > I really don't understand. > As long as the thread sleeps, it does nothing. It also doesn't update the label. In addition, under WinXP, if the thread sleeps longer - or does anything else - it is not even possible to force immediate repaint. See 3rd paragraph: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/messagesandmessagequeues/aboutmessagesandmessagequeues.asp For a short timeout you don't have to care about it. Armin Thank you Armin for your very useful answer. Now I understood the
problem. You solution works great but, I think that I will change the approach. Maybe is better if a use a timer component so all the other components (menu, button, etc) will continue to work while waiting for a new question (this is what they delay was for). Again a big thank you to you. you can use application.doevents() which will allow things to continue,
especially useful if instead of calling sleep you called a function like this: public sub Wait(byval i as integer) while(i >= 0) System.threading.sleep(5) Application.DoEvents() i-=5 end while end sub this will allow the program to continue responding and yet will wait around for your 2000 ms as required.
Show quote
Hide quote
"Dave" <davesa***@gmail.com> schrieb The timer that ringhio mentioned is a better solution than DoEvents. There > you can use application.doevents() which will allow things to > continue, especially useful if instead of calling sleep you called a > function like this: > > public sub Wait(byval i as integer) > while(i >= 0) > System.threading.sleep(5) > Application.DoEvents() > > i-=5 > end while > end sub > > this will allow the program to continue responding and yet will wait > around for your 2000 ms as required. > is already a message loop and no need for another one. DoEvents can cause code reentrance that has to be dealt with additionally. Armin Dave & Armin thank you for your help.
I just finished my little program for helping 5-6 years old children to learn additions. I used the timer solution. I don't want children to use mouse so I'm using the KeyPressed method of the form itself (of course KeyPreview is set to True in this form) .. Timer solution had the problem that Keyboard remained active during the waiting. So I added a Me.KeyPreview = False before the timer sleeping period and a Me.KeyPreview = True after the sleeping period. In this way menu are still working (the menu and the buttons are just for configuration and for exiting or for resetting some right-wrong counters). I just wanted to share this "idea" with you. Thanks again for helping me. Ringhio
Access member variable directly or through property within the class it is defined?
Convert Words in Strings to Caps Is it better to use Try or to use Resume Next? Talk to me about debugging Settings.Settings CLS-complaint axWebBrowswer Context Menu (VB 2003) bitblt Function in VB 2005 Remote DNS Zone Creation with WMI and VB.NET VB2005 - Databound MaskedTextBox controls lose mask in MdiChild Forms. |
|||||||||||||||||||||||