Member Login
member
passwd
remember me on this computer.

- join now -

Search

Neat Stuff

Visit our shop for nerds in control lifestyle products.

Cool stuff
Select a topic of interest:
...and press:
Fortune
CHUBBY CHECKER just had a CHICKEN SANDWICH in downtown DULUTH!
RSS Feed
RSS feed Use this link to get an RSS feed of the Control.com article flow, for private, non-commercial use only:
www.control.com/rss
Select a Page Style
Select one of the following styles:
- BluFu
- Classic
(cookies required)
from the Automation List department...
Visual Basic timer without pausing loop
Software in Automation topic
advertisement
Posted by Wade Greene on 18 March, 2003 - 2:59 pm
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

Posted by V Bravo on 18 March, 2003 - 5:33 pm
You have a real time clock.
Just freeze the value, and compare it, aginst the desired delay + the freezed time.

keep in touch!
VBravo.
vbravo@intelligentes.com
http://www.intelligentes.com/PLCProgramming/main.htm

Posted by David Forsyth on 18 March, 2003 - 5:46 pm
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.

Posted by Todd on 19 March, 2003 - 4:42 pm
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

Posted by L. Donovan Heinz on 22 March, 2003 - 10:18 am
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.

Posted by Scot Sutherland on 19 March, 2003 - 5:09 pm
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.

Posted by Bob Peterson on 19 March, 2003 - 5:26 pm
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

Posted by John on 23 May, 2003 - 2:02 pm
The question was about the use of timers in vb... not is vb a suitable choice for an application.

Posted by Scott Ritchie on 25 March, 2003 - 5:49 pm
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

Posted by Paul Vosselmans on 27 May, 2003 - 5:18 pm
Dear Wade,

See attached link:

<http://support.microsoft.com/default.aspx?scid=http://support. microsoft.com:80/support/kb/articles/Q231/2/98.asp&NoWebContent=1>

Works all right!

Regards,

Paul Vosselmans

Posted by Eric Vosselmans on 26 June, 2003 - 12:15 am
Thx for the link... works all right indeed.

Posted by Dan on 9 September, 2003 - 3:51 pm
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

Posted by RufusVS on 13 September, 2003 - 4:24 pm
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

Posted by R calligan on 14 September, 2003 - 8:20 am
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.

Posted by Anonymous on 24 October, 2003 - 11:51 pm
did you try to do the .Refresh method in the textbox before doing the Sleep(4000) command?

From Control Engineering magazine...
Related articles from Control Engineering magazine
Above articles copyright 2008 Reed Business Information. Subject to its Terms of Use.

Your use of this site is subject to the terms and conditions set forth under Legal Notices and the Privacy Policy. Please read those terms and conditions carefully. Subject to the rights expressly reserved to others under Legal Notices, the content of this site and the compilation thereof is © 1999-2008 Control Technology Corporation. All rights reserved.

Users of this site are benefiting from open source technologies, including PHP, PostgreSQL and Apache. Be happy.

Advertisement
Our Advertisers
Help keep our servers running...
Patronize our advertisers!