代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Diagnostics;
namespace PerfCounterDemo
{
class Program
{
static void Main(string[] args)
{
if (!PerformanceCounterCategory.Exists("ConsolePerfCategory"))
{
PerformanceCounterCategory.Create("ConsolePerfCategory",
"My category description.",
PerformanceCounterCategoryType.SingleInstance,
"ConsolePerf",
"console performace counter.");
}
else
{
Console.WriteLine("ConsolePerfCategory already exists");
}
PerformanceCounter counter = new PerformanceCounter("ConsolePerfCategory", "ConsolePerf",false);
Thread.Sleep(10000);
Console.WriteLine("start");
double ret = 0;
double increment = 0.0;
for (; ; increment += 0.05)
{
ret = Math.Sin(increment) * 40 + 50;
ret = ret < 0 ? 0 : ret;
Console.WriteLine(ret);
counter.RawValue = (long)ret;
Thread.Sleep(200);
}
}
}
}
效果图:
补充:
性能计数器监视器的进入方式:
运行->perfmon->添加->选择计数器名称即可