Click here to Skip to main content
13,197,799 members (40,635 online)
Rate this:
 
Please Sign up or sign in to vote.
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

    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 15-Nov-12 9:38am
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

  Permalink  
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
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
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
DinoRondelly 16-Nov-12 0:30am
   
Hey Thanks again, this returns alot of information and is very useful. I am fairly new to vb.net so bare with me if somewhere I missed something and you already supplied this information but when i was executing these query with WQL Query editor i could seem to get the CPU usage % back at all. Is that possible?
Sergey Alexandrovich Kryukov 16-Nov-12 11:53am
   
Did you mean "could not get"? As I did not 100% understand your concern, I added some links with more detailed code samples; please see my updated answer after [EDIT]. Does it make is more clear?
--SA
DinoRondelly 16-Nov-12 13:12pm
   
Sorry, yes I meant i could not get the cpu % usage per each process is there a way to get that? I will look through the links you posted thank you so much for your help.
Sergey Alexandrovich Kryukov 16-Nov-12 14:57pm
   
Please do. If you have difficulties to implement memory usage polling, your follow-up questions are welcome; I'll see what I can do. If it can resolve your problem, please accept the answer formally (green button).
--SA
DinoRondelly 16-Nov-12 15:24pm
   
Thanks again, I was able to get what i was looking for.

You have been a great help
DinoRondelly 16-Nov-12 15:31pm
   
Also the link below really helped me test everything when querying

http://wqlqueryrunner.codeplex.com/
Sergey Alexandrovich Kryukov 16-Nov-12 15:43pm
   
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web04 | 2.8.171020.1 | Last Updated 16 Nov 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100