私はここで何か非常に悪いことをしているに違いありません。次のようにカスタム パフォーマンス カウンターを作成します。
string counterCategory = "Test Category";
string counterName = "Test Counter";
if (!PerformanceCounterCategory.Exists(counterCategory))
{
Console.WriteLine("Creating Counters");
CounterCreationDataCollection counterCreationDataCollection =
new CounterCreationDataCollection();
counterCreationDataCollection.Add(
new CounterCreationData(counterName,
"Description",
PerformanceCounterType.NumberOfItems32)
);
PerformanceCounterCategory.Create(counterCategory,
"My category description/Help",
PerformanceCounterCategoryType.SingleInstance,
counterCreationDataCollection);
}
カウンター カテゴリとカウンターが作成され、パフォーマンス モニターで表示できるようになります。
次に、カウンターの値を変更しようとします
PerformanceCounter myCounter =
new PerformanceCounter(counterCategory, counterName, false);
for (int i = 0; i < 10; i++)
{
Console.WriteLine("Setting to "+i);
myCounter.RawValue = i;
Thread.Sleep(200);
}
myCounter.Close();
ただし、パフォーマンス モニターでカウンターを座って見ていると、何も起こらず、値が変化することはありません。
それで、私は何を間違っていますか?
nextValue() または rawValue() への呼び出しを追加すると、期待どおりにその値が返されますが、Windows パフォーマンス モニターには依然として平坦な線が表示されます。
for (int i = 0; i < 10; i++)
{
Console.WriteLine("Setting to "+i);
myCounter.IncrementValue()
Console.WriteLine("Next Value = "+myCounter.RawValue());
Thread.Sleep(200);
}
編集:パフォーマンス モニターを閉じて、カウンターを削除せずに再度開くと、新しい値があることに突然気付くことがわかりました。したがって、値が設定されて永続化されますが、パフォーマンス モニターには変更が表示されません。