The source code in this issue is designed for Visual Basic 5.0 and above. It can be used with some small modifications in VB 3 and 4.
This issue shows how to implement a machine independent delay function.
This routine is useful in your applications where you want to wait a certain number of seconds for input. Some good examples are during communications programs or waiting for input. The Wait function allows you to abort it and determine how many seconds it has already been waiting for.
An interesting aspect of this routine you should notice is that the last two parameters are passed by reference. This means that if the values of these variables change anyplace in your program (in this routine or outside of this routine) they will always reflect the current value. This is how we are able to abort the Wait routine from outside of the actual routine. See the sample below for an implementation of this feature.
Public Function Wait(ByVal lSeconds As Long, _ Optional ByRef bAbortFlag As Boolean = False, _ Optional ByRef lElapsed As Long) As Boolean
True means the function timed out (meaning the number of seconds specified by lSeconds has elapsed). False if the bAbortFlag is set to True.
This example will show how to use the Wait function in a situation where your software is dialing a phone number. It assumes you have a form with two Command Buttons on it (cmdCancel and cmdDial). It also assumes you have a Timer control (tmrStatusUpdate) and a Label control (lblStatus). This example will wait 45 seconds for the call to be completed or aborted.
Dim mbAbortFlag As Boolean ' Used to cancel the delay process Dim mlElapsed As Long ' Used to track the number of ' elapsed seconds during the pause Private Sub cmdCancel_Click() mbAbortFlag = True End Sub Private Sub cmdDial_Click() ' ' Dial a phone number.... ' mbAbortFlag = False If Not Wait(45, mbAbortFlag, mlElapsed) Then ' In this example aborted could mean the connection completed ' or the user cancelled it. MsgBox "Aborted." Else ' Abort the dialing process since the timer has elapsed. MsgBox "Timed out." End If End Sub Private Sub tmrStatusUpdate_Timer() lblStatus = mlElapsed & " seconds have elapsed." End Sub
To see the source code for this issue you must be a subscriber to Code of the Week. If you are a subscriber the source code is available at the following address: http://www.codeoftheweek.com/membersonly/bi/0036.html
That concludes this issue of COTW. We hope you find the source code useful in your development.
The below describes the ways you can supply us some feedback about COTW. We would like to see our members help mold COTW into the best Visual Basic source code resource available. But to do that we need your feedback about what you like and what you do not like about COTW.
We have entered a partnership with VBXtras to provide many development tools available for sale on our web site. For a limited time we are offering a FREE one year subscription for anyone who purchases any products through our web site. If you are currently shopping for some cool tools, check out http://www.codeoftheweek.com/catalog.html
This area is new and does not yet have all products online. If there is something particular you are looking for (and can't find it on our site), please let us know. We probably can get it for you at a great price!
If you are interested in advertising in COTW please email us at sponsor@codeoftheweek.com Our rates are VERY reasonable, actually they are almost FREE. We reach over three thousand Visual Basic developers each week.
If you have any suggestions for topics you would like to see covered or questions about this issue, please email them to info@codeoftheweek.com or use online feedback form at http://www.codeoftheweek.com/feedback.html.
If you have any source code you would like to submit for possible inclusion in COTW, please fill out our online submission form at http://www.codeoftheweek.com/submission.html.
Thank you for trying Code of the Week for Visual Basic.
Your free trial expires after you receive your fourth issue. If you want to continue to receive Code of the Week you can get 52 issues of COTW for only $19.95. This is a full year of Visual Basic source code and information to help with all your development. So don't wait, subscribe now! The quickest way to subscribe is to jump to our online order form at http://www.codeoftheweek.com/order.html