Results 1 to 5 of 5

Thread: Find out how long a program has been running

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2002
    Posts
    55

    Find out how long a program has been running

    Hi all,

    Is there an API where you can find out how long a program/process has been running? I know you can use GetTickCount for how long Windows has been active, and was wondering if there was a similar API call for all programs/processes?

    Thanks

  2. #2
    Hyperactive Member wordracr's Avatar
    Join Date
    Aug 2001
    Posts
    281
    I'm not sure. As an alternative, you could store the time when your program starts, and then compare that to the current time..

    Code:
    Option Explicit
    Dim ProgStartTime As Date
    
    Private Sub Command1_Click()
        MsgBox DateDiff("s", ProgStartTime , Now)
    End Sub
    
    Private Sub Form_Load()
        theTime = Now
    End Sub

  3. #3

    Thread Starter
    Member
    Join Date
    Jul 2002
    Posts
    55
    Hi,

    Thanks but I'm trying to find out how long a different app (which may have been running even before my app starts) has been running.

  4. #4
    PowerPoster
    Join Date
    Jul 2002
    Location
    Dublin, Ireland
    Posts
    2,148
    If you have a handle to the thread you can use GetThreadTimes:

    VB Code:
    1. Private Type FILETIME
    2.     dwLowDateTime As Long
    3.     dwHighDateTime As Long
    4. End Type
    5. Private Declare Function GetThreadTimes Lib "kernel32" (ByVal hThread As Long, lpCreationTime As FILETIME, lpExitTime As FILETIME, lpKernelTime As FILETIME, lpUserTime As FILETIME) As Long

    Hope this helps,
    Duncan

  5. #5
    Frenzied Member
    Join Date
    Jul 2002
    Posts
    1,370
    Handle to the process gives you:
    Code:
    Private Type FILETIME
            dwLowDateTime As Long
            dwHighDateTime As Long
    End Type
    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
    Private Declare Function GetProcessTimes Lib "kernel32" (ByVal hProcess As Long, lpCreationTime As FILETIME, lpExitTime As FILETIME, lpKernelTime As FILETIME, lpUserTime As FILETIME) As Long
    Private Declare Function FileTimeToLocalFileTime Lib "kernel32" (lpFileTime As FILETIME, lpLocalFileTime As FILETIME) As Long
    Private Declare Function FileTimeToSystemTime Lib "kernel32" (lpFileTime As FILETIME, lpSystemTime As SYSTEMTIME) As Long
    Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
    Private Sub Form_Load()
        Dim FT0 As FILETIME, FT1 As FILETIME, ST As SYSTEMTIME
        GetProcessTimes GetCurrentProcess, FT1, FT0, FT0, FT0
        FileTimeToLocalFileTime FT1, FT1
        FileTimeToSystemTime FT1, ST
        MsgBox "This program was started at " + CStr(ST.wHour) + ":" + CStr(ST.wMinute) + "." + CStr(ST.wSecond) + " on " + CStr(ST.wMonth) + "/" + CStr(ST.wDay) + "/" + CStr(ST.wYear)
    End Sub
    Use DateDiff to get the up time.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width