VB.net sleep problems

R

ringhio

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)
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.
 
A

Armin Zingler

ringhio said:
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

labNote.Refresh


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.

If you don't add Refresh, the label won't update before the thread is idle.
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/d...ssagequeues/aboutmessagesandmessagequeues.asp

For a short timeout you don't have to care about it.


Armin
 
R

ringhio

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

Dave

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

Armin Zingler

Dave said:
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.

The timer that ringhio mentioned is a better solution than DoEvents. There
is already a message loop and no need for another one. DoEvents can cause
code reentrance that has to be dealt with additionally.


Armin
 
R

ringhio

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top