none
Difficulty building Progressbar based on performanceCounter % Processor Time in Visual Basic

    Question

  • I'm attempting to create a progressBar which will appear in a "Loading!" form that shows up prior to the success or failure confirmation.

    This progressbar, in its current state, loads the form based on a timer.tick. The load process lasts about 4 seconds.

    I'm trying to create a progressbar that will load based on the remaining processing time for the application instance using PerformanceCounter for % Processor Time.

    This is what I have so far, it's wrong, and incomplete of course:

    'Declares public class checking
    Public Class checking
    
        Dim i As Integer
    
        Private Function get_cpu_usage(AppName As String)
            Dim appCPU As New PerformanceCounter("Processor", "% Processor Time", "*** IE8.0CT Alpha", ".")
            Return appCPU.NextValue()
        End Function
    
        'Event handler for form load, sets parameters for progress bar
        Private Sub checking_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            ProgressBar1.Style() = ProgressBarStyle.Marquee
            'ProgressBar1.Maximum = 100
            'ProgressBar1.Value = 0
            Timer1.Interval = 100
            Timer1.Start()
        End Sub
    
        'Sets timer proprieties and duration of progress bar
        Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
            ProgressBar1.Value -= get_cpu_usage("******* 8.0 Configuration Tool 2013.1.3")
            If ProgressBar1.Value = 0 Then
                Timer1.Stop()
                Me.Visible = False
                Me.Close()
            End If
        End Sub
    
        'Closes form and terminates all subroutines
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
            Me.Close()
            main_container.Focus()
        End Sub
    
    End Class

    I think performance counter for % Processor Time returns a decimal value, something like 20.2, or 14.5. I'm not sure if it's incremental or decremental.

    All I would like to do, is for my progressbar to accurately represent the progress of my application loading. I thought that remaining processing time for the progress tick was a good option to represent that in vb.


    • Edited by CodeNemesis Monday, July 29, 2013 2:40 PM code correction
    Monday, July 29, 2013 2:39 PM

Answers

  • Hi CodeNemesis,

    According to your code and description, i think that there are some errors in your code.

    In my side, i fixed your code(Display total processor time, you can change it as you wish):

    Public Class Form1
    
        Private Function get_cpu_usage() As String
            Dim appCPU As New PerformanceCounter("Processor", "% Processor Time", "_Total")
            Dim per As Long
            per = appCPU.NextValue()
            While per = 0
                per = appCPU.NextValue()
            End While
            Return per.ToString("0.0")
        End Function
    
        'Event handler for form load, sets parameters for progress bar
        Private Sub checking_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            'ProgressBar1.Style() = ProgressBarStyle.Marquee
            ProgressBar1.Maximum = 100
            ProgressBar1.Minimum = 0
            ProgressBar1.Value = 0
            Timer1.Interval = 100
            Timer1.Start()
        End Sub
    
        'Sets timer proprieties and duration of progress bar
        Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
            ProgressBar1.Value = get_cpu_usage()
    
            If ProgressBar1.Value = 0 Then
                Timer1.Stop()
                Me.Visible = False
                Me.Close()
            End If
        End Sub
    
        'Closes form and terminates all subroutines
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
            Me.Close()
            'main_container.Focus()
        End Sub
    
    End Class
    

    Screenshot:

    Here are some references for your question:

    #PerformanceCounter Class
    http://msdn.microsoft.com/en-us/library/system.diagnostics.performancecounter.aspx

    #ProgressBar Class
    http://msdn.microsoft.com/en-us/library/system.windows.forms.progressbar.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1


    Franklin Chen
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, July 31, 2013 3:32 AM
    Moderator