-
Jan 20th, 2014, 01:11 AM
#1
Thread Starter
Lively Member
[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,
Last edited by green.pitch; Jan 30th, 2014 at 11:50 PM.
Reason: Resolved
-
Jan 20th, 2014, 01:51 AM
#2
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.
-
Jan 20th, 2014, 02:41 AM
#3
Thread Starter
Lively Member
Re: Performance counter Error in Windows 7
-
Jan 20th, 2014, 04:26 AM
#4
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.
-
Jan 20th, 2014, 04:40 AM
#5
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
-
Jan 20th, 2014, 05:25 AM
#6
Thread Starter
Lively Member
Re: Performance counter Error in Windows 7
Originally Posted by ident
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
-
Jan 20th, 2014, 09:36 AM
#7
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.
-
Jan 20th, 2014, 10:18 AM
#8
Hyperactive Member
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
-
Jan 20th, 2014, 05:09 PM
#9
Re: Performance counter Error in Windows 7
vb Code:
Imports System.Management Public Class Form1 Private Const WIN32_CPU_QUERY As String = "SELECT * FROM Win32_Processor" ''' <summary> ''' Using WMI to get details of the system central processing unit usage usage. ''' </summary> ''' <returns>The details of the image process file path.</returns> Private Function EmurateCentralProcessingUnitUsage() As String Dim centralProcessingUnitUsage As String = Nothing Using managementObjectSearcher As New ManagementObjectSearcher(WIN32_CPU_QUERY) Using managementObjectSearcherResults = managementObjectSearcher.Get() _ .Cast(Of ManagementObject)() _ .SingleOrDefault If managementObjectSearcherResults IsNot Nothing Then centralProcessingUnitUsage = String.Format("CPU {0}%", managementObjectSearcherResults("LoadPercentage")) End If End Using End Using Return centralProcessingUnitUsage End Function End Class
-
Jan 20th, 2014, 11:56 PM
#10
Thread Starter
Lively Member
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.
Originally Posted by ident
vb Code:
Imports System.Management Public Class Form1 Private Const WIN32_CPU_QUERY As String = "SELECT * FROM Win32_Processor" ''' <summary> ''' Using WMI to get details of the system central processing unit usage usage. ''' </summary> ''' <returns>The details of the image process file path.</returns> Private Function EmurateCentralProcessingUnitUsage() As String Dim centralProcessingUnitUsage As String = Nothing Using managementObjectSearcher As New ManagementObjectSearcher(WIN32_CPU_QUERY) Using managementObjectSearcherResults = managementObjectSearcher.Get() _ .Cast(Of ManagementObject)() _ .SingleOrDefault If managementObjectSearcherResults IsNot Nothing Then centralProcessingUnitUsage = String.Format("CPU {0}%", managementObjectSearcherResults("LoadPercentage")) End If End Using End Using Return centralProcessingUnitUsage End Function 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.
-
Jan 21st, 2014, 12:30 AM
#11
Re: Performance counter Error in Windows 7
Originally Posted by green.pitch
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.
-
Jan 21st, 2014, 03:06 AM
#12
Thread Starter
Lively Member
Re: Performance counter Error in Windows 7
Originally Posted by jmcilhinney
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,
-
Jan 21st, 2014, 09:58 AM
#13
Re: Performance counter Error in Windows 7
Can you mark the thread resolved if your question has been answered.
-
Jan 31st, 2014, 01:27 AM
#14
Thread Starter
Lively Member
Re: Performance counter Error in Windows 7
Originally Posted by ident
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|