473,472 Members | 2,193 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

VB.net sleep problems

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.

Feb 22 '06 #1
5 2679
"ringhio" <zi*****@gmail.com> schrieb
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/de...sagequeues.asp

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

Feb 22 '06 #2
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.

Feb 22 '06 #3
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.

Feb 22 '06 #4
"Dave" <da*******@gmail.com> schrieb
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

Feb 22 '06 #5
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

Feb 23 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: ritterhaus | last post by:
Just a simple bit of code to toggle between two state at intervals... import time for i in range(4): print 'On' time.sleep(1) print 'Off' time.sleep(1) .... SHOULD toggle On and Off four...
21
by: Alo Sarv | last post by:
Hi From what I have understood from various posts in this newsgroup, writing event loops pretty much comes down to this: while (true) { handleEvents(); sleep(1); // or _sleep() or...
3
by: kiplring | last post by:
Suppose a function which has Sleep() method in it. And I want to recycle it. I made two buttons which call "Resume()" and "Suspend()". But It doesn't work. The state of thread "t" will be...
3
by: Stephen Miller | last post by:
I have an ASP.Net application that sends a NetworkStream to a .Net Service, which has a TcpListener listening on a port for the ASP.Net client. When it receives a request it creates a new thread...
4
by: Susan | last post by:
On our production server, we have a great deal of data. To improve performance, we give the user a "page" of data at a time and then spawn count of the total number of products based on the query...
7
by: multicherry | last post by:
Hi, Having searched for a way to fetch a window object by name, all I came across were answers along the line of... "All you have to do is say windowObj = window.open("blah", "name");" which...
7
by: davidst95 | last post by:
Hello, I have a program that runs a long process. I tried to add a thread to check if the use hit a cancel button to abort the process. Dim t As Threading.Thread t = New...
0
by: Tim Golden | last post by:
Lowell Alleman wrote: Well you've certainly picked a ticklish area to run into problems with ;). First, forget about the threading aspects for the moment. AFAICT the smallest program which...
4
by: =?Utf-8?B?YzY3NjIyOA==?= | last post by:
Hi all, We use Payflow Pro from Verisign(Now it calls paypal Payflow Pro) as a gateway software to process credit card payment. Most of time, Paypal server is OK and we don't have problems for...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.