/// <summary> /// Dispose(bool disposing) executes in two distinct scenarios. /// If disposing equals true, the method has been called directly /// or indirectly by a user's code. Managed and unmanaged resources /// can be disposed. /// If disposing equals false, the method has been called by the /// runtime from inside the finalizer and you should not reference /// other objects. Only unmanaged resources can be disposed. /// </summary> protected virtual void Dispose(bool disposing) { // Check to see if Dispose has already been called. if (!_Disposed) { // If disposing equals true, dispose all managed // and unmanaged resources. if (disposing) { // Dispose managed resources. _CounterSetInstance.Dispose(); _CounterSet.Dispose(); } // Note disposing has been done. _Disposed = true; } }
static void Main(string[] args) { var schemaPath = RegisterCounters(); PerformanceCounter pc = new PerformanceCounter("Typing", "Words Typed In Interval"); typingCounterSet = new CounterSet(providerId, typingCounterSetId, CounterSetInstanceType.Single); try { typingCounterSet.AddCounter(1, CounterType.Delta32, "Words Typed In Interval"); typingCsInstance = typingCounterSet.CreateCounterSetInstance("Typing Instance"); typingCsInstance.Counters[1].Value = 0; System.Diagnostics.Debug.Assert(pc.RawValue == 0); typingCsInstance.Counters["Words Typed In Interval"].Increment(); System.Diagnostics.Debug.Assert(pc.RawValue == 1); } finally { typingCounterSet.Dispose(); UnregisterCounters(schemaPath); Directory.Delete(Path.GetDirectoryName(schemaPath), true); } }