财讯中国

PerformanceCounter找不到引用怎么办?PerformanceCounter 基本介绍

来源:互联网 2020-12-17 18:09:34

一 PerformanceCounter 基本介绍

1 简单介绍

表示 Windows NT 性能计数器组件

命名空间:System.Diagnostics

程序集:System(在 system.dll 中)

2 构造函数(只介绍本文要用到的)

PerformanceCounter (String, String, String)

功能:

初始化 PerformanceCounter 类的新的只读实例,

并将其与本地计算机上指定的系统性能计数器或自定义性能计数器及类别实例关联

参数说明:

public PerformanceCounter (

string categoryName,

string counterName,

string instanceName

)

categoryName

性能计数器关联的性能计数器类别(性能对象)的名称。

counterName

性能计数器的名称。

instanceName

性能计数器类别实例的名称,或者为空字符串 ("")(如果该类别包含单个实例)。

二 示例方法:

需要引用命名空间

usingSystem.Diagnostics;

usingSystem.Threading;

usingSystem.Collections;

1 获取性能计数器类别列表

虽然系统中有很多可用的计数器类别,但与之交互最频繁的可能是“Cache”(缓存)、“Memory”(内存)、

“Objects”(对象)、“PhysicalDisk”(物理磁盘)、“Process”(进程)、“Processor”(处理器)、

“Server”(服务器)、“System”(系统)和“Thread”(线程)等类别

publicstaticvoidGetCategoryNameList()

{

PerformanceCounterCategory[]myCat2;

myCat2=PerformanceCounterCategory.GetCategories();

for(inti=0;i

{

Console.WriteLine(myCat2[i].CategoryName.ToString());

}

}

2 获取性能计数器类别下的实例的名称实例下的性能计数器的名称

publicstaticvoidGetInstanceNameListANDCounterNameList(stringCategoryName)

{

string[]instanceNames;

ArrayListcounters=newArrayList();

PerformanceCounterCategorymycat=newPerformanceCounterCategory(CategoryName);

try

{

instanceNames=mycat.GetInstanceNames();

if(instanceNames.Length==0)

{

counters.AddRange(mycat.GetCounters());

}

else

{

for(inti=0;i

{

counters.AddRange(mycat.GetCounters(instanceNames[i]));

}

}

for(inti=0;i

{

Console.WriteLine(instanceNames[i]);

}

Console.WriteLine("******************************");

foreach(PerformanceCountercounterincounters)

{

Console.WriteLine(counter.CounterName);

}

}

catch(Exception)

{

Console.WriteLine("Unabletolistthecountersforthiscategory");

}

}

3 根据categoryName,counterName,instanceName获得性能情况显示

privatestaticvoidPerformanceCounterFun(stringCategoryName,stringInstanceName,stringCounterName)

{

PerformanceCounterpc=newPerformanceCounter(CategoryName,CounterName,InstanceName);

while(true)

{

Thread.Sleep(1000);//waitfor1second

floatcpuLoad=pc.NextValue();

Console.WriteLine("CPUload="+cpuLoad+"%.");

}

}

4 调用方法3显示cpu使用率

PerformanceCounterFun("Processor","_Total","%ProcessorTime");

ServiceModelPerformanceCounters.dll文件下载,解决找不到ServiceModelPerformanceCounters.dll的问题

ServiceModelPerformanceCounters.dll控件常规安装方法(仅供参考):

一、如果在运行某软件或编译程序时提示缺少、找不到ServiceModelPerformanceCounters.dll等类似提示,您可将从脚本之家下载来的ServiceModelPerformanceCounters.dll拷贝到指定目录即可(一般是system系统目录或放到软件同级目录里面),或者重新添加文件引用。

二、您从我们网站下载下来文件之后,先将其解压(一般都是rar压缩包), 然后根据您系统的情况选择X86/X64,X86为32位电脑,X64为64位电脑。默认都是支持32位系统的, 如果您不知道是X86还是X64,您可以看这篇文章。

三、根据软件情况选择文件版本。此步骤比较复杂,如果是Windows的dll文件,

版本号以5.0开头的或含有 nt 一般是windows2000的文件。

版本号以5.1开头的或含有 xp、xpsp1、xpsp2、xpsp3 信息的一般是windowsXP的文件。

版本号以6.0开头的或含有 longhorn、vista 信息的一般是windowsVista的文件。

版本号以6.1开头的或含有 win7 信息的一般是windows7的文件。 如果不是windows的dll文件,则需要灵活查看版本号、描述、网友提供的信息、以及相关dll的版本号去判断。

四、直接拷贝该文件到系统目录里:

1、Windows 95/98/Me系统,将ServiceModelPerformanceCounters.dll复制到C:\Windows\System目录下。

2、Windows NT/2000系统,将ServiceModelPerformanceCounters.dll复制到C:\WINNT\System32目录下。

3、Windows XP/WIN7/win10系统(64位系统对应64位dll文件,32位系统对应32位dll文件),将ServiceModelPerformanceCounters.dll复制到C:\Windows\System32目录下。

4、如果您的系统是64位的请将32位的dll文件复制到C:\Windows\SysWOW64目录

五、打开"开始-运行-输入regsvr32 ServiceModelPerformanceCounters.dll",回车即可解决。

标签:PerformanceCounter

相关新闻