Solved

marking time in vb

Posted on 2000-04-18
7
248 Views
Last Modified: 2012-08-14
i want to mark time to as much precision
as possible.  how do i do it in vb?

thanks.
senardji
0
Comment
Question by:senardji
7 Comments
 
LVL 18

Expert Comment

by:deighton
Comment Utility
Dim dStart As Double

dStart = Timer
MsgBox "!"
'.
'.
'.
Debug.Print Timer - dStart

0
 
LVL 6

Expert Comment

by:MELeBlanc
Comment Utility
   Private Declare Sub GetSystemTime Lib "kernel32" (lpSystemTime As SYSTEMTIME)
 
    Private Type SYSTEMTIME
        wYear As Integer
        wMonth As Integer
        wDayOfWeek As Integer
        wDay As Integer
        wHour As Integer
        wMinute As Integer
        wSecond As Integer
        wMilliseconds As Integer
    End Type
   
    Dim TimeUnformatted As SYSTEMTIME

   


    GetSystemTime TimeUnformatted
    MsgBox TimeUnformatted.wMilliseconds


The above code will do an API call to get the system time broken down into the user defined data type SYSTEMTIME.  At that point you can work with timestamps down to the millisecond level.

Hope this helps.
0
 
LVL 12

Expert Comment

by:mark2150
Comment Utility
Although you'll find a lot of controls denominating time in units of Ms you'll find that in reality there is no underlying timer mechanism that will provide a timer tick any tighter than the 18.2Hz time-of-day tick. This gives you a granularity of about 55Ms. Any control that says it can measure time tighter than this on a plain (no special hardware) PC is simply *LYING* to you.

There will be several that will attack this answer, but I've got the original IBM technical reference manual at my elbow and both complete schematics and BIOS listings and no-one has *EVER* proved me wrong on this - and many have tried.

PC's cannot measure time tighter than 55Ms. Period.

You *CAN* add specialty hardware to do it. You can by timer counter cards or tap into the sound card hardware, but on a *plain PC*, no, nope, noway, nohow.

M
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 3

Expert Comment

by:Foyal
Comment Utility
Singles or Doubles are no good for precision because they get screwed up in their binary representations when copied to memory. (try formatting a few to see). For the best precision possible with VB set your variables that hold your numbers to Variant Type and sub type them using the CDec Function in order to use the Decimal Data Type. Like this:

Milliseconds = CDec(TicksorWhatever)

This data type is good to about 24 decimal places and stays uncorrupted since it is represented in memory as Integers. Also, another trick is to clear the floating point registers of your processor by calling something like D = Int(D) just before doing any floating point math.

Hope this helps,
Foyal
0
 
LVL 1

Accepted Solution

by:
mathies earned 50 total points
Comment Utility
Look that:


' ============================================================================
' Module:    CTimer
'
'   Stopwatch type timer class
'
' Notes:
' ----------------------------------------------------------------------------
'
' History:
'
'   07/--/1999  web  initial version
'
' ============================================================================
'
Option Explicit

Private Declare Function timeGetTime Lib "winmm.dll" () As Long
Private Declare Function timeGetDevCaps Lib "winmm.dll" (lpTimeCaps As TIMECAPS, ByVal uSize As Long) As Long
Private Declare Function timeBeginPeriod Lib "winmm.dll" (ByVal uPeriod As Long) As Long
Private Declare Function timeEndPeriod Lib "winmm.dll" (ByVal uPeriod As Long) As Long

Private Type TIMECAPS
   wPeriodMin As Long
   wPeriodMax As Long
End Type

Private Const TIMER_STOPPED = 0
Private Const TIMER_RUNNING = 1
Private Const PERIOD_NOT_SET = 0

' multimedia timer constants adapted from MMSYSTEM.H

Private Const TIMERR_BASE = 96
Private Const TIMERR_NOERROR = 0                    ' no error
Private Const TIMERR_NOCANDO = TIMERR_BASE + 1      ' request not completed
Private Const TIMERR_STRUCT = TIMERR_BASE + 33      ' time struct size

Private mlngStartTime       As Long
Private mlngAccumulatedTime As Long
Private mlngRunning         As Long
Private mlngPeriod          As Long
Private mtcPeriods          As TIMECAPS

Private Sub Class_Initialize()

    mlngRunning = TIMER_RUNNING         ' the timer starts up running
    mlngPeriod = PERIOD_NOT_SET

    ' get the min & max periods at initialization since they don't change

    timeGetDevCaps mtcPeriods, Len(mtcPeriods)

    ' reset the timer to start cleanly

    Reset

End Sub

Private Sub Class_Terminate()

    ' make sure the timer period is released

    If mlngPeriod <> PERIOD_NOT_SET Then
        timeEndPeriod mlngPeriod
    End If

End Sub

'-------------------------------------------------------------------------------
' properties
'-------------------------------------------------------------------------------

' ElapsedTime - return the time elapsed since the timer was last reset

Public Property Get ElapsedTime() As Long

    ElapsedTime = mlngAccumulatedTime - mlngStartTime + timeGetTime() * mlngRunning

End Property

Public Property Get MinPeriod() As Long

    MinPeriod = mtcPeriods.wPeriodMin

End Property

Public Property Get MaxPeriod() As Long

    MaxPeriod = mtcPeriods.wPeriodMax

End Property

Public Property Get Period() As Long

    Period = mlngPeriod

End Property

'-------------------------------------------------------------------------------
' methods
'-------------------------------------------------------------------------------

' BeginPeriod - set timing period

Public Sub BeginPeriod(Period As Long)
    Dim lngReturnCode   As Long

    If mlngPeriod <> PERIOD_NOT_SET Then
        Err.Raise 51, "CStopWatchMM.BeginPeriod", "Period is already set, EndPeriod call required first"
    End If

    With mtcPeriods
        If Period < .wPeriodMin Then
            Err.Raise 51, "CStopWatchMM.BeginPeriod", "Period less than minimum"
        End If
        If Period > .wPeriodMax Then
            Err.Raise 51, "CStopWatchMM.BeginPeriod", "Period greater than maximum"
        End If
    End With

    lngReturnCode = timeBeginPeriod(Period)

    If lngReturnCode = TIMERR_NOERROR Then
        mlngPeriod = Period
    Else
        mlngPeriod = PERIOD_NOT_SET
        Err.Raise 51, "CStopWatchMM.BeginPeriod", "Error setting the multimedia timer period"
    End If

End Sub

' EndPeriod - release timing period

Public Sub EndPeriod(Period As Long)
    Dim lngReturnCode   As Long

    If mlngPeriod = PERIOD_NOT_SET Then
        Err.Raise 51, "CStopWatchMM.EndPeriod", "Period not set, BeginPeriod call required first"
    End If

    ' period must match the BeginPeriod call

    If Period <> mlngPeriod Then
        Err.Raise 51, "CStopWatchMM.EndPeriod", "Period does not match call to BeginPeriod"
    End If

    lngReturnCode = timeEndPeriod(Period)

    mlngPeriod = PERIOD_NOT_SET

    If lngReturnCode <> TIMERR_NOERROR Then
        Err.Raise 51, "CStopWatchMM.EndPeriod", "Error releasing the multimedia timer period"
    End If

End Sub

' Reset - reset the timer to zero (does not change the running state)

Public Sub Reset()
   
    mlngAccumulatedTime = 0
    mlngStartTime = timeGetTime()

End Sub

' StartTiming - restart the timer (after StopTiming) and accumulate time

Public Sub StartTiming()

    If mlngRunning = 0 Then
        mlngStartTime = timeGetTime()
        mlngRunning = TIMER_RUNNING
    End If

End Sub

' StopTiming - stop the timer so time doesn't accumulate

Public Sub StopTiming()

    If mlngRunning <> 0 Then
        mlngAccumulatedTime = mlngAccumulatedTime + timeGetTime() - mlngStartTime
        mlngStartTime = 0
        mlngRunning = TIMER_STOPPED
    End If

End Sub
0
 
LVL 12

Expert Comment

by:mark2150
Comment Utility
Mathies solution looks good but (as I said originally) is dependent on the presence of the sound card to support the multi-media functions. While this is certainly a common enough peripherial, it is not on 100% of the machines out in the world...

M
0
 
LVL 1

Expert Comment

by:mathies
Comment Utility
If you want to have more details on how develop more precision timer.
Go to www.vbpj.com and look the article
of april 2000 on timer and you will found all the information how to build you timer.

There no much manner to use timer.
1) The vb timer control
2) API timer control. But It`s only   give you the same precision than the VB control but you don`t need a form to use it.
3) The answer I gave you before.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now