subroutine with timer requirement

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a sub which needs to do some things then wait a specified length of
time before going on. I tried adding a timer with counter in its tick event
then checking the state of the counter in the subprocedure. But this doesn't
work:

dim progress as single

Sub mysub
stuff

timer1.enabled = true
while progress <1
textbox1.text=progress
end while

more stuff
end sub

timer1 tick event handler
progress = progress +.01
end

The textbox shows me that nothing is happening. The program hangs up.

What's the right way to do this? Should this be a threading application?
 
Hi,

Try something like this.

timer1.enabled = true


more stuff
end sub

timer1 tick event handler
progress = progress +.01
textbox1.text=progress.tostring
timer1.enabled = progress<1
end


Ken
------------------------
I have a sub which needs to do some things then wait a specified length of
time before going on. I tried adding a timer with counter in its tick event
then checking the state of the counter in the subprocedure. But this doesn't
work:

dim progress as single

Sub mysub
stuff

timer1.enabled = true
while progress <1
textbox1.text=progress
end while

more stuff
end sub

timer1 tick event handler
progress = progress +.01
end

The textbox shows me that nothing is happening. The program hangs up.

What's the right way to do this? Should this be a threading application?
 
Can you not just use a While loop with the DateTime.Now to wait a certain
number of seconds?
 
I've done this several times. The best way to do it is use
System.Threading.Thread.CurrentThread.Sleep(<Number of miliseconds to
sleep>)

for example System.Threading.Thread.CurrentThread.Sleep(2000) will sleep for
two seconds.

This works for both single process apps as well as multi-threaded apps.

John
 
<<Can you not just use a While loop with the DateTime.Now to wait a certain
number of seconds? >>

This would just kill CPU performance. Best way is using
system.threading.thread.sleep()

John
 
You are correct and i'm sure the thread.sleep is the best way to go. I'm
used to VB3 programming and didn't have the thread class. I used the Now
function a lot in a restaurant cash register program that we used in our
restaurant and along with DoEvents, the CPU was able to keep up with
continuous ringing up of tickets and storing them in a database...faster than
the waitress could enter the tickets from two cash registers. Still learning
VB.Net though.
 
Mark,

You are now using the procedure with the loop and the sleep
"threading.thread.sleep(seconds)" and you combine that with a complete other
method as Ken has showed you.

They both should work but not both together.

Show us what you made in real code from the part Ken showed you in a kind of
pseudo, maybe we can help you than.

Cor
 
Cor,

Threading.thread.sleep is milliseconds not seconds.

Ken
-------------
Mark,

You are now using the procedure with the loop and the sleep
"threading.thread.sleep(seconds)" and you combine that with a complete other
method as Ken has showed you.

They both should work but not both together.

Show us what you made in real code from the part Ken showed you in a kind of
pseudo, maybe we can help you than.

Cor
 
All of the methods cited will work to a greater or lesser degree but none
will work completely. There is no way to get exact timing in a virtual
system. If you really need EXACT timing then you need to be much closer to
the iron. I don't think that .NET was ever intended to be used as a real
time system. Yes, you can find the exact time now but you can not tell the
system the exact time when you want to resume because it may not be paying
any attention to you at that time. Probably the most noticeable thing that
will interfere is garbage collection but there are others.
BobJ
 
Ken,
Threading.thread.sleep is milliseconds not seconds.
Did you sand this to prickle me? I corrected that message that much to not
completly messed up your message that I did not look at that part, however
to reassure you, I knew it already, was a typo.

However thanks to make me attent on it.

:-)))

Cor
 

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

Back
Top