Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
 
TitleRecord a program's usage
Keywordsusage
CategoriesSoftware Engineering
 
Store ellapsed time in a file (or the system Registry). Load and display it when the program starts. Update it in the form's Unload event handler.
 
Private StartTime As Date

' Load and display the program's usage information.
Private Sub LoadUsageInfo()
Dim fnum As Integer
Dim usage As Date

    ' Open the file and read the value.
    fnum = FreeFile
    On Error GoTo NoFile
    Open App.Path & "\usage.dat" For Input As fnum
    Input #fnum, usage
    Close fnum
    
    ' Display the current usage.
NoFile:
    On Error GoTo 0
    lblUsage.Caption = Format$(usage, "hh:mm:ss")
End Sub

' Save the new total usage.
'
' Note that this routine is not really safe for
' multiple instances. Another instance of the
' program could possibly update the usage file
' between the time we read it anad the time we
' rewrite it. If this is an issue, lock another
' file while we need exclusive access to this one
' so no other process can get in.
Private Sub UpdateUsageInfo()
Dim fnum As Integer
Dim usage As Date

    ' Open the file and read the value. This is
    ' important in case someone else used the
    ' program while we were using it.
    fnum = FreeFile
    On Error GoTo NoFile
    Open App.Path & "\usage.dat" For Input As fnum
    Input #fnum, usage
    Close fnum

    ' Add the usage for this run.
NoFile:
    On Error GoTo 0
    usage = usage + (Now - StartTime)

    ' Rewrite the file.
    Open App.Path & "\usage.dat" For Output As fnum
    Write #fnum, usage
    Close fnum
    
    ' Update StartTime. This is useful if the
    ' program updates the total usage more than once.
    StartTime = Now
End Sub

Private Sub Form_Load()
    ' Record the start time for this session.
    StartTime = Now

    LoadUsageInfo
End Sub

Private Sub Form_Unload(Cancel As Integer)
    UpdateUsageInfo
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated