Member Login
Search
Past & Future Posts
Sponsored Communities
Neat Stuff

Visit our shop for nerds in control lifestyle products.
Cool stuff
Thermal Overload
The threads that wouldn't die...
- PC reliability?
- Windows, real time
- PID loops
- PCs vs. PLCs
- Replacing people
- MS 'monopoly'?
- Software quality
- Where do we go from here?
- Why pay?
- PC reliability?
- Windows, real time
- PID loops
- PCs vs. PLCs
- Replacing people
- MS 'monopoly'?
- Software quality
- Where do we go from here?
- Why pay?
Fortune
CHUBBY CHECKER just had a CHICKEN SANDWICH in downtown DULUTH!
RSS Feed
www.control.com/rss
from the Automation List department...
Visual Basic timer without pausing loopI 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
Thanks
Wade
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
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
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.
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
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
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.
Thanks a lot.
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.
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.
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
Bob Peterson
The question was about the use of timers in vb... not is vb a suitable choice for an application.
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
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
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
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
Thx for the link... works all right indeed.
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
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
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.
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.
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- You need 2 monitors: This Website will prove it
- Digital manufacturing: Autodesk Inventor shows component interactions
- Software certified by AT&T: Runs on PDAs, cellphones, enables mobile applications
- Research: HMI supervisory software use increases with service needs
- Listen in: Watching a migration in progress
- HMI/SCADA: Microsoft Fluent technology helps enhance Proficy iFIX 5.0
- Eliminating diagnostic delays
- Speedier production management application development
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.
Our Advertisers
Help keep our servers running...
Patronize our advertisers!
Patronize our advertisers!



