Results 1 to 6 of 6

Thread: Running time of Active Application...

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2005
    Posts
    438

    Running time of Active Application...

    Hello,

    I am wondering if it is possible to get the name of active application and its start time.

    I have used following code to get all process but not sure how to get current active process and its start time.
    Will be thankful for any responses.

    Code:
     Dim myProcessList As Process() = Process.GetProcesses
    
     For Each myProcess As Process In myProcessList
    
                If myProcess.MainWindowTitle <> "" Then
    
                    Dim myListView As ListViewItem = ListView1.Items.Add(myProcess.MainWindowTitle)
                    myListView.SubItems.Add("running")
    
                End If
    
            Next

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    24,034

    Re: Running time of Active Application...

    Try this...

    Code:
    Dim proc As Process = Process.GetCurrentProcess
    Dim ts As TimeSpan = Now - proc.StartTime
    MsgBox($"Uptime = {ts.Hours:00}:{ts.Minutes:00}:{ts.Seconds:00}.{ts.Milliseconds}")
    Last edited by .paul.; Jun 13th, 2021 at 02:49 AM.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2005
    Posts
    438

    Re: Running time of Active Application...

    Hello Paul,

    Thank you for your response.

    Tried the same but I am getting "Form1" only even if I bring some other app - say Notepad into foreground.

    Here is my sample logic..

    thanks

    Code:
      Private Sub GetActiceProcess()
    
            ListView1.Items.Clear()
    
            Dim myProcess As Process = Process.GetCurrentProcess
    
            Dim myTs As TimeSpan = Now - myProcess.StartTime
            Dim myTime As Long = myTs.TotalSeconds
    
            Dim myListItem As ListViewItem = ListView1.Items.Add(myProcess.MainWindowTitle.ToString)
            myListItem.SubItems.Add(myProcess.StartTime)
            myListItem.SubItems.Add(myTime)
    
        End Sub
    
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            Call GetActiceProcess()
        End Sub
    [/QUOTE]

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    24,034

    Re: Running time of Active Application...

    Code:
    Dim myProcessList As Process() = Process.GetProcesses
    
    For Each myProcess As Process In myProcessList
    
        If myProcess.MainWindowTitle <> "" Then
    
            Dim myListView As ListViewItem = ListView1.Items.Add(myProcess.MainWindowTitle)
            myListView.SubItems.Add("running")
    
            Dim myTs As TimeSpan = Now - myProcess.StartTime
            Dim myTime As Long = myTs.TotalSeconds
            ' add to listview, or whatever
    
        End If
    
    Next

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2005
    Posts
    438

    Re: Running time of Active Application...

    Hi Paul,

    I am able to read all running processes without any problem; the challenge I have is to read the current process or active process that a user is currently active on.

    thanks...


    Quote Originally Posted by .paul. View Post
    Code:
    Dim myProcessList As Process() = Process.GetProcesses
    
    For Each myProcess As Process In myProcessList
    
        If myProcess.MainWindowTitle <> "" Then
    
            Dim myListView As ListViewItem = ListView1.Items.Add(myProcess.MainWindowTitle)
            myListView.SubItems.Add("running")
    
            Dim myTs As TimeSpan = Now - myProcess.StartTime
            Dim myTime As Long = myTs.TotalSeconds
            ' add to listview, or whatever
    
        End If
    
    Next

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    24,034

    Re: Running time of Active Application...

    Code:
    Public Declare Function GetActiveWindow Lib "user32.dll" () As IntPtr
    Code:
    Dim proc As Process = Process.GetProcessById(CInt(GetActiveWindow))

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