2 AverageTimer32性能计数器

时间:2012-07-01 10:17:07

标签: c# .net windows console-application performancecounter

如何在一个应用程序(.exe)中定义和使用两个AverageTimer32性能计数器?

附加说明: 我正在努力测量在操作的不同部分上花费的时间。

我有一个AverageTimer32性能计数器和一个AverageBase。它有效。

现在我想添加第二个AverageTimer32性能计数器。即使我添加另一个AverageBase计数器,它也不起作用。我想我错了什么。代码示例非常感谢。

这是代码的不同部分:

_AverageDuration = new PerformanceCounter();
_AverageDuration.CategoryName = CategoryName;
_AverageDuration.CounterName = "average time per operation";
_AverageDuration.MachineName = ".";
_AverageDuration.ReadOnly = false;

_AverageDurationBase = new PerformanceCounter();
_AverageDurationBase.CategoryName = CategoryName;
_AverageDurationBase.CounterName = "average time per operation base";
_AverageDurationBase.MachineName = ".";
_AverageDurationBase.ReadOnly = false;
...
CounterCreationDataCollection counters = new CounterCreationDataCollection();
...
CounterCreationData avgDuration = new CounterCreationData();
avgDuration.CounterName = "average time per operation";
avgDuration.CounterHelp = "Average duration per operation execution";
avgDuration.CounterType = PerformanceCounterType.AverageTimer32;
counters.Add(avgDuration);

CounterCreationData avgDurationBase = new CounterCreationData();
avgDurationBase.CounterName = "average time per operation base";
avgDurationBase.CounterHelp = "Average duration per operation execution base";
avgDurationBase.CounterType = PerformanceCounterType.AverageBase;
counters.Add(avgDurationBase);
...
PerformanceCounterCategory.Create(CategoryName, "Category for Receiver", PerformanceCounterCategoryType.Unknown, counters);
...
AverageDuration.IncrementBy(performanceCounterwatch.ElapsedTicks);
AverageDurationBase.Increment();

1 个答案:

答案 0 :(得分:3)

添加它们的顺序至关重要。您必须添加平均值,然后是相应的基数,然后是第二个平均值,然后是第二个相应的基数。