1

如何在一个应用程序 (.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();
4

1 回答 1

3

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

于 2012-07-01T10:21:10.887 回答