Results 1 to 14 of 14

Thread: [RESOLVED] Performance counter Error in Windows 7

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2012
    Posts
    106

    Resolved [RESOLVED] Performance counter Error in Windows 7

    Hello guys,
    I'm trying to get the total cpu usage in VB.net 2010 with the help of Performance Counter.

    Code:
    Public Class Form1
        Dim cpu As New PerformanceCounter()
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            With cpu
                .CategoryName = "Processor"
                .CounterName = "% Processor Time"
                .InstanceName = "_Total"
            End With
            Me.Text = cpu.NextValue.ToString
        End Sub
    End Class
    Above code is working fine on my computer, but when i'm running it on another computer (in windows 7 ultimate) i'm getting error. Please see the attached snapshot.

    How can I solve this error ?

    Regards,

    Name:  Untitled.png
Views: 927
Size:  33.9 KB
    Last edited by green.pitch; Jan 30th, 2014 at 11:50 PM. Reason: Resolved

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Performance counter Error in Windows 7

    The same code runs fine for me too, although on Win 8.1. Perhaps you could get all the exception information, including the stack trace, and post that. We might be able to get a better idea of what's going wrong from that. Use a Try...Catch block to catch the exception and just call its ToString method to get that information.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Feb 2012
    Posts
    106

    Re: Performance counter Error in Windows 7

    Quote Originally Posted by jmcilhinney View Post
    The same code runs fine for me too, although on Win 8.1. Perhaps you could get all the exception information, including the stack trace, and post that. We might be able to get a better idea of what's going wrong from that. Use a Try...Catch block to catch the exception and just call its ToString method to get that information.
    Yes Sure,
    Name:  err1.png
Views: 733
Size:  46.7 KB

    Name:  err2.png
Views: 687
Size:  36.8 KB

    Name:  err3.png
Views: 706
Size:  29.8 KB

  4. #4
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: Performance counter Error in Windows 7

    I have read some where before there is a bug in the PerformanceCounter Class that Microsoft is aware of. This was way back in 2009 so can't see why it's not been fixed. I think it generally requires cleaning the corrupt registry items.

  5. #5
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: Performance counter Error in Windows 7

    What happens when you try to compile your application with this line.

    Code:
    Dim memory As New PerformanceCounter("Memory", "Available MBytes")
    How to manually rebuild Performance Counter Library values

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Feb 2012
    Posts
    106

    Re: Performance counter Error in Windows 7

    Quote Originally Posted by ident View Post
    What happens when you try to compile your application with this line.

    Code:
    Dim memory As New PerformanceCounter("Memory", "Available MBytes")
    How to manually rebuild Performance Counter Library values
    Memory is getting without any error.
    Now I can get CPU usage value on that same pc after running a command "lodctr /R".

    But i'm still not satisfied with this solution. If there is any alternate method to get total cpu usage % without performance counter, please tell me.

    Thanks

  7. #7
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: Performance counter Error in Windows 7

    You can use the WMI class. I have not got time now to write an example, but if no body has by tonight i will write it for you. There is actually a WMI generator.

  8. #8
    Hyperactive Member
    Join Date
    Jun 2013
    Posts
    312

    Re: Performance counter Error in Windows 7

    Try it a little differently. Let me know if this works, I've not tested it fully but I see no reason for it not to. This uses 3 labels to return the values and 2 timers, and 3 progress bars. It's better to use 2 timers for performance accuracy. Code is self explanatory, let me know if it helps.

    Code:
    'Timer Tick for system monitor.
    
        Dim cpu As Integer
        Dim ram As Integer
        Dim Cprogressbar As Integer
        Private Sub TimeMonitor_Tick(sender As Object, e As EventArgs) Handles TimeMonitor.Tick
            cpu = CPUPerformanceCounter.NextValue
            ram = RAMPerformanceCounter.NextValue
            Cprogressbar = HHDCPerformanceCounter.NextValue
        End Sub
        Private Sub TimeMonitor2_Tick(sender As Object, e As EventArgs) Handles TimeMonitor2.Tick
            Try
                'Count CPU 
                If CPUProgressBar.Value < cpu Then
                    CPUProgressBar.Value += 1
                ElseIf CPUProgressBar.Value > cpu Then
                    CPUProgressBar.Value -= 1
                End If
                'And now RAM 
                If RAMProgressBar.Value < ram Then
                    RAMProgressBar.Value += 1
                ElseIf RAMProgressBar.Value > ram Then
                    RAMProgressBar.Value -= 1
                End If
                'Now HDD C:\\
                If CDProgressBar.Value < Cprogressbar Then
                    CDProgressBar.Value += 1
                ElseIf CDProgressBar.Value > Cprogressbar Then
                    CDProgressBar.Value -= 1
                End If
                CPULevelLabel.Text = CPUProgressBar.Value.ToString + "% Used"
                RAMLevelLabel.Text = RAMProgressBar.Value.ToString + "% Used"
                HDCLevelLabel.Text = CDProgressBar.Value.ToString + "% Free"
                'Now IP4 Stuff
            Catch ProgressStats As Exception
                MsgBox(ProgressStats.Message)
            End Try
        End Sub

  9. #9
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: Performance counter Error in Windows 7

    vb Code:
    1. Imports System.Management
    2.  
    3. Public Class Form1
    4.  
    5.     Private Const WIN32_CPU_QUERY As String = "SELECT * FROM Win32_Processor"
    6.  
    7.     ''' <summary>
    8.     ''' Using WMI to get details of the system central processing unit usage usage.
    9.     ''' </summary>
    10.     ''' <returns>The details of the image process file path.</returns>
    11.     Private Function EmurateCentralProcessingUnitUsage() As String
    12.         Dim centralProcessingUnitUsage As String = Nothing
    13.  
    14.         Using managementObjectSearcher As New ManagementObjectSearcher(WIN32_CPU_QUERY)
    15.             Using managementObjectSearcherResults = managementObjectSearcher.Get() _
    16.                                                     .Cast(Of ManagementObject)() _
    17.                                                     .SingleOrDefault
    18.  
    19.                 If managementObjectSearcherResults IsNot Nothing Then
    20.                     centralProcessingUnitUsage = String.Format("CPU {0}%", managementObjectSearcherResults("LoadPercentage"))
    21.                 End If
    22.             End Using
    23.         End Using
    24.  
    25.         Return centralProcessingUnitUsage
    26.     End Function
    27.  
    28. End Class

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Feb 2012
    Posts
    106

    Re: Performance counter Error in Windows 7

    @Mucker, Thank you but as i told above, I'm getting errors on using Performance Counter. Hence i'm looking to get CPU usage % without using performance counter.


    Quote Originally Posted by ident View Post
    vb Code:
    1. Imports System.Management
    2.  
    3. Public Class Form1
    4.  
    5.     Private Const WIN32_CPU_QUERY As String = "SELECT * FROM Win32_Processor"
    6.  
    7.     ''' <summary>
    8.     ''' Using WMI to get details of the system central processing unit usage usage.
    9.     ''' </summary>
    10.     ''' <returns>The details of the image process file path.</returns>
    11.     Private Function EmurateCentralProcessingUnitUsage() As String
    12.         Dim centralProcessingUnitUsage As String = Nothing
    13.  
    14.         Using managementObjectSearcher As New ManagementObjectSearcher(WIN32_CPU_QUERY)
    15.             Using managementObjectSearcherResults = managementObjectSearcher.Get() _
    16.                                                     .Cast(Of ManagementObject)() _
    17.                                                     .SingleOrDefault
    18.  
    19.                 If managementObjectSearcherResults IsNot Nothing Then
    20.                     centralProcessingUnitUsage = String.Format("CPU {0}%", managementObjectSearcherResults("LoadPercentage"))
    21.                 End If
    22.             End Using
    23.         End Using
    24.  
    25.         Return centralProcessingUnitUsage
    26.     End Function
    27.  
    28. End Class

    Thanks ident, but i'm getting any error on your code -
    'Cast' is not a member of 'System.Management.ManagementObjectCollection'.
    Last edited by green.pitch; Jan 21st, 2014 at 12:06 AM.

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Performance counter Error in Windows 7

    Quote Originally Posted by green.pitch View Post
    Thanks ident, but i'm getting any error on your code -
    'Cast' is not a member of 'System.Management.ManagementObjectCollection'.
    That's a LINQ method so requires at least .NET 3.5, a reference to System.Core.dll and that the System.Linq namespace be imported.

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Feb 2012
    Posts
    106

    Smile Re: Performance counter Error in Windows 7

    Quote Originally Posted by jmcilhinney View Post
    That's a LINQ method so requires at least .NET 3.5, a reference to System.Core.dll and that the System.Linq namespace be imported.

    Thanks alot jmcilhinney. It is what i was looking for...
    Regards,

  13. #13
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: Performance counter Error in Windows 7

    Can you mark the thread resolved if your question has been answered.

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Feb 2012
    Posts
    106

    Re: Performance counter Error in Windows 7

    Quote Originally Posted by ident View Post
    Can you mark the thread resolved if your question has been answered.
    Sorry i forgot. Now it's done

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