Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

Cross-midnight time measurements

Total Hit ( 3045)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


Click here to download the attached file  


Any time the behavior of your code depends on the Timer function you should take into account the (more or less) remote possibility that your code is executed just before midnight. Take for example the following code:

Click here to copy the following block
Sub Pause(seconds as Single)
  Dim initTime as Single
  initTime = Timer
  Do: Loop Until Timer >= initTime + seconds
End Sub

If this code is executed at 23:59:59 (or even earlier, depending on the value of the seconds argument), the Do-Loop will never end, and the user will have to kill the program from the Task Manager. The worst facet of this problem is that the bug can manifest months or even years after you've installed the program, which would leave you clueless about its causes.
Here's the fix for the above routine:

Click here to copy the following block
Sub Pause(seconds as Single)
  Dim initTime As Single, currTime As String
  initTime = Timer
  Do
    currTime = Timer
  Loop Until currTime >= initTime + seconds Or (currTime < initTime And _
    currTime > initTime + seconds - 86400)
End Sub

If you don't need the pause to be really precise, and you're satisfied with an integer number of seconds, you can also use this simpler approach:

Click here to copy the following block
Sub Pause(seconds as Single)
  Dim initTime as Date
  initTime = Now
  Do: Loop Until DateDiff("s", initTime, Now) >= seconds
End Sub

An even simpler way to insert a pause that work across midnite is by means of the Sleep API function. See the link below to learn how to use this function.


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.