Click here to Skip to main content
15,882,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have been asked to build something like the task manager, I need to get a list of processes and how much of the cpu they are using in % value.

These dont need to be running counts i just need what percentage of the cpu was being used when my application started up.

I am getting the cpu percentage using the below code

VB
Public Shared Function GetCpuUsage() As Integer

    Dim myCount As Integer
    Using cpu = New PerformanceCounter()

        With cpu
            .CategoryName = "Processor"
            .CounterName = "% Processor Time"
            .InstanceName = "_Total"
        End With

        myCount = cpu.NextValue()
        System.Threading.Thread.Sleep(1000)
        myCount = cpu.NextValue()
        System.Threading.Thread.Sleep(1000)
        myCount = cpu.NextValue()
    End Using

    Return myCount
End Function


The question I have is how would i get the % value of each process that is using any cpu usage and ignore anything that is at 0?
Posted

 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 15-Nov-12 22:03pm    
That is something else, also useful stuff, a 5.
--SA
Espen Harlinn 16-Nov-12 3:25am    
Thank you, Sergey :-D
For such purposes, you can use WMI, or System.Management,
http://msdn.microsoft.com/en-us/library/system.management.aspx[^].

This is a nice and short introductory article:
http://www.csharphelp.com/2006/10/wmi-made-easy-for-c/[^].

This is what you will need to query CPU usage:
http://msdn.microsoft.com/en-us/library/windows/desktop/aa394277%28v=vs.85%29.aspx[^].

[EDIT]

There is a number of code samples with some cookbook recipes for WMI query which you can easily find if you Google; for example:
http://bit.ly/QMh4rI[^].

Just one relevant code sample:
http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/469ec6b7-4727-4773-9dc7-6e3de40e87b8/[^].

And this is the CodeProject article on the topic:
http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/469ec6b7-4727-4773-9dc7-6e3de40e87b8/[^].

I hope it's enough for you to solve your problem.

Good luck,
—SA
 
Share this answer
 
v3
Comments
Espen Harlinn 15-Nov-12 19:43pm    
Well answered :-D
DinoRondelly 15-Nov-12 19:45pm    
Thank you very much this will help with a few problems
Sergey Alexandrovich Kryukov 15-Nov-12 22:02pm    
Thank you, Espen.
--SA
DinoRondelly 15-Nov-12 21:55pm    
this was exactly what i needed !!!!!
Sergey Alexandrovich Kryukov 15-Nov-12 22:01pm    
Great. -- You are very welcome.
Good luck, call again.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900