Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 140 Vote(s) - 3.51 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to get the CPU Usage in C#?

#1
I want to get the overall total CPU usage for an application in C#. I've found many ways to dig into the properties of processes, but I only want the CPU usage of the processes, and the total CPU like you get in the TaskManager.

How do I do that?
Reply

#2
CMS has it right, but also if you use the server explorer in visual studio and play around with the performance counter tab then you can figure out how to get lots of useful metrics.
Reply

#3
You can use WMI to get CPU percentage information. You can even log into a remote computer if you have the correct permissions. Look at

[To see links please register here]

to get an idea of what you can accomplish.

Also helpful might be the MSDN reference for the [Win32_Process][1] namespace.

See also a CodeProject example [How To: (Almost) Everything In WMI via C#][2].


[1]:

[To see links please register here]

[2]:

[To see links please register here]

Reply

#4
This class automatically polls the counter every 1 seconds and is also thread safe:

public class ProcessorUsage
{
const float sampleFrequencyMillis = 1000;

protected object syncLock = new object();
protected PerformanceCounter counter;
protected float lastSample;
protected DateTime lastSampleTime;

/// <summary>
///
/// </summary>
public ProcessorUsage()
{
this.counter = new PerformanceCounter("Processor", "% Processor Time", "_Total", true);
}

/// <summary>
///
/// </summary>
/// <returns></returns>
public float GetCurrentValue()
{
if ((DateTime.UtcNow - lastSampleTime).TotalMilliseconds > sampleFrequencyMillis)
{
lock (syncLock)
{
if ((DateTime.UtcNow - lastSampleTime).TotalMilliseconds > sampleFrequencyMillis)
{
lastSample = counter.NextValue();
lastSampleTime = DateTime.UtcNow;
}
}
}

return lastSample;
}
}
Reply

#5
I did not like having to add in the 1 second stall to all of the `PerformanceCounter` solutions. Instead I chose to use a `WMI` solution. The reason the 1 second wait/stall exists is to allow the reading to be accurate when using a `PerformanceCounter`. However if you calling this method often and refreshing this information, I'd advise not to constantly have to incur that delay... even if thinking of doing an async process to get it.

I started with the snippet from here

[To see links please register here]

and added a full explanation of the solution on my blog post below:

[Get CPU Usage Across All Cores In C# Using WMI][1]


[1]:

[To see links please register here]

Reply

#6
This seems to work for me, an example for waiting until the processor reaches a certain percentage

var cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total");
int usage = (int) cpuCounter.NextValue();
while (usage == 0 || usage > 80)
{
Thread.Sleep(250);
usage = (int)cpuCounter.NextValue();
}

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through