Results 1 to 11 of 11

Thread: System.Diagnostics Problem

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2010
    Posts
    231

    System.Diagnostics Problem

    Hello again,I hope this time someone will reply me - my problem is that when I try to write a exe application for measuring the cpu usage every time I get the error:
    Code:
    A first chance exception of type 'System.InvalidOperationException' occurred in System.dll
    or
    Code:
    A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
    The code I use is :
    Code:
    Imports System.Diagnostics
    
    Dim oPerf1 As New PerformanceCounter
    
    oPerf1.CategoryName = "Processor"
    oPerf1.CounterName = "% Processor Time"
    oPerf1.InstanceName = "0"
    
    Dim I As Integer
    For I = 0 To 100
    SomeListBox.Items.Add (oPerf1.NextValue)
    Threading.Thread.Sleep (20)
    Next
    And I get the above error,I searched a little in the internet but the most post were for database optimization and so on.

  2. #2
    Hyperactive Member
    Join Date
    Dec 2006
    Posts
    293

    Re: System.Diagnostics Problem

    After a quick google search, This measures CPU usage:
    Code:
        Private m_PerformanceCounter As New  _
            System.Diagnostics.PerformanceCounter( _
                "Processor", "% Processor Time", "_Total")
    
        Private Sub btnClear_Click(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles btnClear.Click
            lstCpu.Items.Clear()
        End Sub
    
        Private Sub tmrCheckCpu_Tick(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles tmrCheckCpu.Tick
            lstCpu.Items.Add(CInt(m_PerformanceCounter.NextValue()) _
                & "%")
            lstCpu.SelectedIndex = lstCpu.Items.Count - 1
        End Sub
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            tmrCheckCpu.Start()
        End Sub

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Mar 2010
    Posts
    231

    Re: System.Diagnostics Problem

    But I get the above errors every time on this line:
    Code:
            System.Diagnostics.PerformanceCounter( _
                "Processor", "% Processor Time", "_Total")
    And to be more accurate I get the error line here:
    "Processor"
    "% Processor Time" - CounterName
    "_Total"

    And sometimes its say's me that the CounterName cant be found in the registry.
    Last edited by mitko29; Feb 27th, 2011 at 05:51 AM.

  4. #4
    Hyperactive Member
    Join Date
    Dec 2006
    Posts
    293

    Re: System.Diagnostics Problem

    have you still got this line in?

    Code:
    Imports System.Diagnostics
    If so , comment or take it out. and then try

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Mar 2010
    Posts
    231

    Re: System.Diagnostics Problem

    Quote Originally Posted by crampz View Post
    have you still got this line in?

    Code:
    Imports System.Diagnostics
    If so , comment or take it out. and then try
    No change with it or without this code :
    The error description I get from the program is :
    Code:
    An error occurred creating the form. See Exception.InnerException for details.  The error is: Cannot load Counter Name data because an invalid index '' was read from the registry.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Mar 2010
    Posts
    231

    Re: System.Diagnostics Problem

    Can someone told me from where this problem comes ?

  7. #7
    Hyperactive Member
    Join Date
    Dec 2006
    Posts
    293

    Re: System.Diagnostics Problem

    Could this be any help?
    http://stackoverflow.com/questions/1...-4-0-windows-7

    It seems performance counters were corrupted on my system. Although I didn't follow this post exactly, it led me to the solution. Here is what I did:

    In an command prompt with administrator/elevate privileges typed the following:

    lodctr /?

    Useful stuff in there...

    Then typed:

    lodctr /R

    According to the docs from the prior step, this gets windows to rebuild the perf registry strings and info from scratch based on the current registry settings and backup INI files. I have a feeling this is what did the magic. However, next I noticed the .NET performance counters were not there anymore so based on this I typed the following to reload them:

    lodctr "C:\Windows\Microsoft.NET\Framework64\v4.0.20506\corperfmonsymbols.ini"

    Note that this path is for .NET Framework 4.0 on x64. You can imagine the path for other variations of the framework/platform. I'm guessing you should always load the counters from the highest version of the .NET framework that you have installed, but that is just a guess.

  8. #8
    Junior Member
    Join Date
    Feb 2011
    Posts
    27

    Re: System.Diagnostics Problem

    Tested your code and for VB 2010 Expresss it seems to work without error after making one change. Your Imports statement needs to go before the class declaration and the rest of your code needs to go into some event - in my case I stuck it into the Form_Load event. Once I did that the listbox populated.
    Code:
    Imports System.Diagnostics
    Public Class Form1
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            tmrCheckCPU.Start()
            Dim oPerf1 As New PerformanceCounter
            oPerf1.CategoryName = "Processor"
            oPerf1.CounterName = "% Processor Time"
            oPerf1.InstanceName = "0"
            Dim I As Integer
            For I = 0 To 100
                SomeListBox.Items.Add(oPerf1.NextValue.ToString)
                Threading.Thread.Sleep(20)
            Next
        End Sub
    End Class

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Mar 2010
    Posts
    231

    Re: System.Diagnostics Problem

    Quote Originally Posted by crampz View Post
    Man you are a lifesaver
    But this is not working as I expect - my task manager is showing 9% but my program is showing only zero some help here ?

  10. #10
    Hyperactive Member
    Join Date
    Dec 2006
    Posts
    293

    Re: System.Diagnostics Problem

    Using the code i posted and the code your posted it seems to have a little delay. i set the timer to 500 (half a second) and 100 milliseconds, for instance when the cpu read 17% the program would read 16% one reading after. e.g

    Code:
    10%
    2% < Task manager read 17% here
    16% < Program Read 16% Here
    24%
    If that makes sense

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Mar 2010
    Posts
    231

    Re: System.Diagnostics Problem

    Quote Originally Posted by crampz View Post
    Using the code i posted and the code your posted it seems to have a little delay. i set the timer to 500 (half a second) and 100 milliseconds, for instance when the cpu read 17&#37; the program would read 16% one reading after. e.g

    Code:
    10%
    2% < Task manager read 17% here
    16% < Program Read 16% Here
    24%
    If that makes sense
    Well I rewrite the code a little and now it's perfect:
    Code:
        Imports System.Threading
    Public Class Form1
        Private t_PerformanceCounter As New System.Diagnostics.PerformanceCounter("Processor", "% Processor Time", "_Total")
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            Dim type As String = CInt(t_PerformanceCounter.NextValue()) & "%"
            Label1.Text = type.ToString()
            Threading.Thread.Sleep(20)
        End Sub
    End Class
    And I set the Timer1.interval = 420 and now is reading exactly like the task manager.
    Now the greater headache to rewrite this to vb6 .
    Some help how to convert the system.diagnostic to vb6 some.component ?
    God safe me
    Last edited by mitko29; Feb 27th, 2011 at 04:45 PM.

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