How to get the CPU Usage in C#?

后端 未结 10 2184
醉酒成梦
醉酒成梦 2020-11-22 06:22

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 proce

10条回答
  •  暖寄归人
    2020-11-22 07:14

    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 Returning CPU usage in WMI using C# and added a full explanation of the solution on my blog post below:

    Get CPU Usage Across All Cores In C# Using WMI

提交回复
热议问题