Visual Basic timer without pausing loop

W

Thread Starter

Wade Greene

I am writing a program in VB to control a machine. I know to write it in such a way that it will never "hang." But in parts of the program I need a delay to allow cylinders to extend/retract. Has anyone ever done this before and if so could you please point me in the right direction.

Thanks

Wade
 
D

David Forsyth

look under DoEvents -
putting a DoEvent within a timing loop enables other applications to use computer resources while your VB program waits.

if you use standard VB timers, beware crossing the "midnight divide" and beware daylight savings time, etc.

You might want to look into the windows API for timing instead of VB Now() or timer.

Use Windows 2000 server, or maybe XP, not Windows ME which we found worthless for any pretense of real time control.
 
The API command is:
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Put that statement in a module. Call it by using, for example, "Sleep(1000)" (causes a delay of 1 sec).

-Todd
 
S

Scot Sutherland

Set up a Do loop in Sub Main. Inside that do loop, call all of your "parallel" functions in sequence. Using state logic, you can build timers in the individual functions. You will need to declare Win API function timeGetTime. Here is an example.

Public blnAppIsRunning as Boolean
Public Declare Function timeGetTime Lib "winmm" () As Long
.
.
.
Sub Main
.
.
.
'Initialize stuff
blnAppIsRunning = True
.
.
.
' Main loop

Do
' Function calls
Call SomeFunction1
Call SomeFunction2
.
.
.
Loop Until not blnAppIsRunning

End

End Sub

'Functions & Subs
Public Function SomeFunction1() as Boolean
Static lngStartTime as long
Static intState as integer

Select Case intState
Case 1
' Initialize timer
lngStartTime = timeGetTime
intState = 2
Case 2
' See if 500 msec timer has timed out
' If it has, the state will increase.
' If it has not, flow will drop through
' to "Exit Function."
If timeGetTime > lngStartTime + 500 then
intState = 3
Endif
Case 3
' Do stuff
.
.
.
Case Else
End Select

Exit Function

In your "exit program" routine, set blnAppIsRunning = false to exit the Do loop.
 
B

Bob Peterson

VB is an absolutely awful choice for such an application. there are so many things that can make a VB application go off the deep end, that I would never ever choose it for such an application.

Bob Peterson
 
L

L. Donovan Heinz

I KNEW that there had to be an easier way to do that than setting up a timer with global access. Short delays are such a necessary part of any operating system.

Thanks a lot.
 
S

Scott Ritchie

As some other people have mentioned, be careful using timers in VB. When I have to use a timer, I use a timer object from CCRP, their high performance timers are much more reliable than the timer object that ships with VB.

The other advantage is these timers are class objects, you don't have to drop the timer on a VB form like you do with VB's timer.

Unfortunately, the CCRP timer is not supported for .NET languages but if you're using VB6, I'd recommend CCRP. Go to http://www.mvps.org/ccrp/controls/ccrptimer6.htm and you can get a free download.

Good Luck!

Scott
 
Hmm, not sure if i follow. This is a very basic(non-literal) example the logic im using:

Sub Button_Click()

MessageBox.Caption = "Hello"
Sleep (4000)
MessageBox.Caption = MessageBox.Caption & " World"

End Sub

i want it to execute the first commmand, delay, then execute the second. when i use that logic above, the box is blank for 4 seconds, at the end of which it says, "Hello World"... and yes, I'm quite new to programming/VB.

Any help? Thanks,

-D
 
Dan,

I wrote this quick program which worked just fine. Open a form, create a button, enter this code in the click function.

Private Sub Command1_Click() Command1.Caption = "Hello" EndTime = Timer + 3 While (Timer < EndTime) Wend Command1.Caption = Command1.Caption & " World" End Sub

It may be that calling the timer function allows the screen to update, but your sleep() function doesn't. (Careful, don't run this program just before midnight!)

Rufus
 
The problem with your program is the sleep command does not release control back to the O.system until it reaches the end sub. I normally set up a timer control to increment a global variable and then setup a loop like this
messagebox.caption = "Hello"
while timercount < duration
a= DoEvents()
wend
messagebox.caption = "World"
The doevents command releases control back to the operating system allowing the message box to be painted to the screen. timercount is a variable that is incremented in the timer control
like this.
private sub timer1_timer
timercount=timercount+1
end sub
the timer control and doevents are documented in the help section of visual basic at least in vb5 and vb6 which I am familiar with.
 
Top