Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
 
 
 
 
 
 
 
Old Pages
 
Old Index
Site Map
What's New
 
Books
How To
Tips & Tricks
Tutorials
Stories
Performance
Essays
Links
Q & A
New in VB6
Free Stuff
Pictures
 
 
 
TitleMeasure a process's user, privileged, and total time in VB .NET
Keywordsuser time, privileged time, total time, ellapsed time
CategoriesSoftware Engineering, Tips and Tricks
 
Use the System.Diagnostics.Process namespace's GetCurrentProcess method to get an object containing information about the current process. Use its UserProcessorTime, PrivilegedProcessorTime, and TotalProcessorTime methods to get the process's timing information. as TimeSpans. Use the TimeSpan's Subtract method to find the difference between the values at different times.
 
Imports System.Diagnostics.Process
...
Private Sub btnStart_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnStart.Click
    Static user_start_time As TimeSpan
    Static priv_start_time As TimeSpan
    Static totl_start_time As TimeSpan
    Static user_stop_time As TimeSpan
    Static priv_stop_time As TimeSpan
    Static totl_stop_time As TimeSpan
    Dim proc As Process
    Dim elapsed_time As TimeSpan

    If btnStart.Text = "Start" Then
        lblUserTime.Text = ""
        lblPrivTime.Text = ""
        lblTotlTime.Text = ""
        proc = GetCurrentProcess()
        user_start_time = proc.UserProcessorTime()
        priv_start_time = proc.PrivilegedProcessorTime()
        totl_start_time = proc.TotalProcessorTime()
        btnStart.Text = "Stop"
    Else
        proc = GetCurrentProcess()
        user_stop_time = proc.UserProcessorTime()
        elapsed_time = _
            user_stop_time.Subtract(user_start_time)
        lblUserTime.Text = _
            elapsed_time.TotalSeconds.ToString("0.000000")

        priv_stop_time = proc.PrivilegedProcessorTime()
        elapsed_time = _
            priv_stop_time.Subtract(priv_start_time)
        lblPrivTime.Text = _
            elapsed_time.TotalSeconds.ToString("0.000000")

        totl_stop_time = proc.TotalProcessorTime()
        elapsed_time = _
            totl_stop_time.Subtract(totl_start_time)
        lblTotlTime.Text = _
            elapsed_time.TotalSeconds.ToString("0.000000")

        btnStart.Text = "Start"
    End If
End Sub
 
 
Copyright © 1997-2003 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated