PerformanceCounter.Decrement 方法

定义

通过有效的原子操作使关联的性能计数器减一。Decrements the associated performance counter by one through an efficient atomic operation.

public:
 long Decrement();
public long Decrement ();
member this.Decrement : unit -> int64
Public Function Decrement () As Long

返回

Int64

减少后的计数器值。The decremented counter value.

例外

该计数器是只读的,因而应用程序无法使之减少。The counter is read-only, so the application cannot decrement it.

- 或 --or- 此实例未与性能计数器正确关联。The instance is not correctly associated with a performance counter.

- 或 --or- 使用全局共享内存时,InstanceLifetime 属性设置为 ProcessThe InstanceLifetime property is set to Process when using global shared memory.

访问 API 时出错。An error occurred when accessing a system API.

注解

只能写入自定义计数器。You can write only to custom counters. 所有系统计数器均为只读。All system counters are read-only.

备注

IncrementIncrementByDecrement 方法使用 interlocks 更新计数器值。The Increment, IncrementBy, and Decrement methods use interlocks to update the counter value. 这有助于在多线程或多进程方案中保持计数器值的准确性,但也会导致性能下降。This helps keep the counter value accurate in multithreaded or multiprocess scenarios, but also results in a performance penalty. 如果不需要联锁操作提供的准确性,可以 RawValue 直接更新属性,使性能提高5倍。If you do not need the accuracy that interlocked operations provide, you can update the RawValue property directly for up to a 5 times performance improvement. 但在多线程方案中,对计数器值的某些更新可能会被忽略,导致数据不准确。However, in multithreaded scenarios, some updates to the counter value might be ignored, resulting in inaccurate data.

备注

如果属性的值 InstanceLifetimeProcess ,并且性能计数器类别是使用 .NET Framework 版本1.0 或1.1 创建的,则 InvalidOperationException 会引发。If the value for the InstanceLifetime property is Process and the performance counter category was created with .NET Framework version 1.0 or 1.1, an InvalidOperationException is thrown. 使用早期版本创建的性能计数器类别使用全局共享内存,并且的值 InstanceLifetime 必须是 GlobalPerformance counter categories created with earlier versions use global shared memory, and the value for InstanceLifetime must be Global. 如果在 .NET Framework 的版本1.0 或1.1 上运行的应用程序未使用该类别,请删除并重新创建该类别。If the category is not used by applications running on versions 1.0 or 1.1 of the .NET Framework, delete and recreate the category.

适用于

线程安全性

此方法是线程安全的。This method is thread safe.