如何使用.NET 类获取特定进程的CPU和内存使用情况PerformanceCounter?还有什么区别
Processor\% Processor Time和Process\% Processor Time?
我对这两者感到有些困惑.
如何使用System.Diagnostics.PerformanceCounter跟踪进程的内存和CPU使用情况?
如何使用C#代码获取系统的CPU,RAM和磁盘驱动器使用情况?
我正在开发一个应用程序(.NET 4.0,C#):
1.扫描文件系统.
2.打开并读取一些文件.
该应用程序将在后台运行,应该对磁盘使用率影响较小.如果用户正在执行常规任务并且磁盘使用率很高,则不应该打扰用户.反之亦然,如果没有人使用磁盘,应用程序可以更快.
主要问题是由于使用API(mapi32.dll)来读取文件,我不知道I/O操作的实际数量和大小.如果我要求API做某事我不知道它读取多少字节来处理我的响应.
那么问题是如何监控和管理磁盘使用情况?包括文件系统扫描和文件读取......
检查标准性能监视器工具使用的性能计数器?还是其他任何方式?
需要每1秒执行一次免费的可用内存,所以我使用方法和计时器滴答,但它没有改变,它在标签文本中总是显示8081MB.那么如何每隔1秒检查一次呢?因为使用计算机内存也会改变.这是我的代码:
// Get Available Memory
public void getAvailableRAM()
{
ComputerInfo CI = new ComputerInfo();
ulong mem = ulong.Parse(CI.TotalPhysicalMemory.ToString());
lbl_Avilable_Memory.Text = (mem / (1024 * 1024) + " MB").ToString();
}
private void Form1_Load(object sender, EventArgs e)
{
// Get Available Memory Timer
ram_timer.Enabled = true;
// end memory
}
private void ram_timer_Tick(object sender, EventArgs e)
{
getAvailableRAM();
}
Run Code Online (Sandbox Code Playgroud) 我想在我的项目中拥有当前的CPU利用率
namespace Monitoring_Tool
{
public partial class ajaxExecute : System.Web.UI.Page
{
private PerformanceCounter theCPUCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total");
private PerformanceCounter theMemCounter = new PerformanceCounter("Memory", "Available MBytes");
protected void Page_Load(object sender, EventArgs e)
{
float cpuUsage = 0.00F;
cpuUsage = this.theCPUCounter.NextValue();
}
}
}
Run Code Online (Sandbox Code Playgroud)
当我调试我的项目时,值cpuUsage显示0.00但是当我这样做QuickWatch时this.theCPUCounter.NextValue();它正在显示20.6786852.
为什么我不能将它存储在变量中?
我正在编写一个可以在Windows Startup上运行的应用程序.
当应用程序在我的PC真正解决之前陷入困境时,我无法忍受,这是一个非关键的应用程序.我希望我的应用程序礼貌地等待所有其他启动项目完成并解决,以便用户的PC在开始执行任何工作之前变得响应.
我该如何检测这种情况?我想我可以遍历注册表并查找所有正在运行的启动进程,或者使用长时间计时器.我只是希望我可能会使用另一种不那么狡猾的技巧.
编辑:该应用程序具有UI,无法作为服务运行.它有一个托盘模式.它做了一些图像渲染.
有没有办法在asp.net页面上显示CPU和RAM使用情况统计信息.我试过这段代码,但是我有错误:
Access to the registry key 'Global' is denied.
Run Code Online (Sandbox Code Playgroud)
在这条线上:
ramCounter = new PerformanceCounter("Memory", "Available MBytes");
Run Code Online (Sandbox Code Playgroud) 在我们的应用程序中,我们使用Windows性能计数器来存储我们的一些应用程序指标,这些指标稍后会在某些Web服务中检索.
我遇到了从计数器读取值所花费的时间问题.我已经浏览了我的应用程序的其余部分,一切都很好,性能明智,但从循环中的计数器(从列表或数组)读取需要花费大量的时间.
示例代码:
// This triggers a read of the counter's initial value (1000ms delay following for calculated counters)
counters.ToList().ForEach(counter => counter.NextValue());
Run Code Online (Sandbox Code Playgroud)
在我对上面的循环的测试中,1,359个计数器的列表需要20秒,并且使用秒表,似乎读取计数器值的平均时间是0-10ms,或大约80-90ms.其中很多都需要0ms,最高约为170ms,平均非零值约为80-90ms.
也许我太乐观了,但我认为读取1,000个数值应该只需要几毫秒.这里有更多的处理工作比我知道的还要多吗?
我实际上在我的逻辑中有另一个循环,它获得计算计数器的第二个值.这只会使情况变得更糟.:)
谢谢!
我把计数器检索包裹在一个秒表中,我对结果感到惊讶.阅读即使是简单的属性.RawValue仍然需要花费过多的时间.据我所知,计数器基本上都是一样的,检索应该非常快; 奇怪的是,我也看到了网络类别的计数器需要更长时间的模式.
根据http://joe.blog.freemansoft.com/2014/03/windows-performance-counters.html,性能计数器服务的性能甚至不应该是一个考虑因素.
我已将一些秒表结果发布到以下的pastebin:http://pastebin.com/raw.php?i = aDJk2Tru
我的代码如下:
Stopwatch t;
foreach (var c in counters)
{
t = Stopwatch.StartNew();
var r = c.RawValue;
Debug.WriteLine(t.ElapsedMilliseconds.ToString("000") + " - " + c.CategoryName + ":" + c.CounterName + "(" + c.CounterType + ") = " + r);
}
Run Code Online (Sandbox Code Playgroud)
正如您在粘贴中看到的那样,很多读取都是0,但在50-100ms范围内有很多.我真的不明白这是怎么回事.当然,一个计数器值应该和其他值一样快,对吧?
我一直试图在C#中获得Windows PC(Windows 7运行.Net 4.5)的总CPU使用率.看起来使用PerformanceCounter应该能够满足我的需求.
我根据以下三个链接编写了一些试用代码(并检查了msdn页面),这是最基本的版本:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
namespace EntropyProject
{
class Program
{
static void Main(string[] args)
{
PerformanceCounter cpuCounter;
cpuCounter = new PerformanceCounter();
cpuCounter.CategoryName = "Processor";
cpuCounter.CounterName = "% Processor Time";
cpuCounter.InstanceName = "_Total";
while(true)
{
try
{
float firstValue = cpuCounter.NextValue();
System.Threading.Thread.Sleep(500);
Console.WriteLine("Before getting processor:");
float currentCpuUsage = cpuCounter.NextValue();
Console.WriteLine("After getting processor:");
System.Threading.Thread.Sleep(1000);
Console.WriteLine(currentCpuUsage);
}
catch (Exception e)
{
Console.WriteLine("\n{0}\n", e.Message);
}
System.Threading.Thread.Sleep(10000);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
每当调用NextValue时,都会触发下面的异常错误.这似乎是性能计数器值出现问题的常见问题. …