C# to get total PC CPU usage by using WMI

L

linuxfedora

Hi All,

Is there any sample code on how to get the total PC CPU usage by using
WMI, it will look like the task manager one.

I have tried to use the performanceCounter, but it has permission
problem in the Vista with UAC enabled.

And i have tried to find in the Internet, but seems no complete code
for getting the total PC CPU usage, Thanks
 
A

Arne Vajhøj

linuxfedora said:
Is there any sample code on how to get the total PC CPU usage by using
WMI, it will look like the task manager one.

I have tried to use the performanceCounter, but it has permission
problem in the Vista with UAC enabled.

And i have tried to find in the Internet, but seems no complete code
for getting the total PC CPU usage, Thanks

Something like:

using System;
using System.Management;

public class MainClass
{
public static void Main(string[] args)
{
ObjectQuery wmicpus = new WqlObjectQuery("SELECT * FROM
Win32_Processor");
ManagementObjectSearcher cpus = new
ManagementObjectSearcher(wmicpus);
foreach (ManagementObject cpu in cpus.Get())
{
Console.WriteLine(cpu["DeviceId"] + " : " +
cpu["LoadPercentage"]);
}
}
}

?

Arne
 
L

linuxfedora

I have tried, but it has UnauthorizedAccessException in the cpus.Get()
when i run it?Thanks


linuxfedora said:
Is there any sample code on how to get the total PC CPU usage by using
WMI, it will look like the task manager one.
I have tried to use the performanceCounter, but it has permission
problem in the Vista with UAC enabled.
And i have tried to find in the Internet, but seems no complete code
for getting the total PC CPU usage, Thanks

Something like:

using System;
using System.Management;

public class MainClass
{
     public static void Main(string[] args)
     {
         ObjectQuery wmicpus = new WqlObjectQuery("SELECT * FROM
Win32_Processor");
         ManagementObjectSearcher cpus =  new
ManagementObjectSearcher(wmicpus);
         foreach (ManagementObject cpu in cpus.Get())
         {
             Console.WriteLine(cpu["DeviceId"]  + " : " +
cpu["LoadPercentage"]);
         }
     }

}

?

Arne
 
A

Arne Vajhøj

linuxfedora said:
linuxfedora said:
Is there any sample code on how to get the total PC CPU usage by using
WMI, it will look like the task manager one.
I have tried to use the performanceCounter, but it has permission
problem in the Vista with UAC enabled.
And i have tried to find in the Internet, but seems no complete code
for getting the total PC CPU usage, Thanks
Something like:

using System;
using System.Management;

public class MainClass
{
public static void Main(string[] args)
{
ObjectQuery wmicpus = new WqlObjectQuery("SELECT * FROM
Win32_Processor");
ManagementObjectSearcher cpus = new
ManagementObjectSearcher(wmicpus);
foreach (ManagementObject cpu in cpus.Get())
{
Console.WriteLine(cpu["DeviceId"] + " : " +
cpu["LoadPercentage"]);
}
}

}
I have tried, but it has UnauthorizedAccessException in the cpus.Get()
when i run it?Thanks

The you do not have privs to use WMI either.

Arne
 
L

linuxfedora

linuxfedora said:
linuxfedora wrote:
Is there any sample code on how to get the total PC CPU usage by using
WMI, it will look like the task manager one.
I have tried to use the performanceCounter, but it has permission
problem in the Vista with UAC enabled.
And i have tried to find in the Internet, but seems no complete code
for getting the total PC CPU usage, Thanks
Something like:
using System;
using System.Management;
public class MainClass
{
     public static void Main(string[] args)
     {
         ObjectQuery wmicpus = new WqlObjectQuery("SELECT * FROM
Win32_Processor");
         ManagementObjectSearcher cpus =  new
ManagementObjectSearcher(wmicpus);
         foreach (ManagementObject cpu in cpus.Get())
         {
             Console.WriteLine(cpu["DeviceId"]  + " : " +
cpu["LoadPercentage"]);
         }
     }
}

 > I have tried, but it has UnauthorizedAccessException in the cpus.Get()
 > when i run it?Thanks

The you do not have privs to use WMI either.

Arne

Is it the problem in Vista? Then what method should i use if i have to
get the PC CPU usage in Vista with UAC enabled and in normal user
account?THanks
 
A

Arne Vajhøj

linuxfedora said:
linuxfedora said:
linuxfedora wrote:
Is there any sample code on how to get the total PC CPU usage by using
WMI, it will look like the task manager one.
I have tried to use the performanceCounter, but it has permission
problem in the Vista with UAC enabled.
And i have tried to find in the Internet, but seems no complete code
for getting the total PC CPU usage, Thanks
Something like:
using System;
using System.Management;
public class MainClass
{
public static void Main(string[] args)
{
ObjectQuery wmicpus = new WqlObjectQuery("SELECT * FROM
Win32_Processor");
ManagementObjectSearcher cpus = new
ManagementObjectSearcher(wmicpus);
foreach (ManagementObject cpu in cpus.Get())
{
Console.WriteLine(cpu["DeviceId"] + " : " +
cpu["LoadPercentage"]);
}
}
}
I have tried, but it has UnauthorizedAccessException in the cpus.Get()
when i run it?

The you do not have privs to use WMI either.
Is it the problem in Vista? Then what method should i use if i have to
get the PC CPU usage in Vista with UAC enabled and in normal user
account?THanks

I don't know. You will need a more Windows knowledgeable person
than me to answer that question.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top