取消性能计数器的注册

本文关键字:注册 性能计数器 取消 | 更新日期: 2023-09-27 17:50:11

我已经为我的应用程序添加了性能自定义计数器操作发生的次数。

但是现在我想注销它。是否可以注销它,删除它。

我已经在c#中使用了这个计数器。

创建性能类别和的类为它添加计数器。

using System;
using System.Diagnostics;
namespace GroupChatLibrarySamples
{
    ///<summary>
    ///Helper class to demonstrate the setup of performance counters.
    ///</summary>
    public class PerformanceCounterHelper
    {
        //Total number of tests executed
        private PerformanceCounter _TotalFiles = null;
        /// <summary>
        /// Constructor
        /// </summary>
        public PerformanceCounterHelper()
        {
            // Set up the performance counter(s)
             if (!PerformanceCounterCategory.Exists("Total_Files","."))
             {
                //Create the collection container
                CounterCreationDataCollection counters = new CounterCreationDataCollection();
                ////Create counter #1 and add it to the collection
                CounterCreationData tests = new CounterCreationData();
                tests.CounterName = "Total_Files_Sent";
                tests.CounterHelp = "Total number of Files Sent";
                tests.CounterType = PerformanceCounterType.NumberOfItems32;
                counters.Add(tests);
                //Create the category and all of the counters.
                PerformanceCounterCategory.Create("Total_Files", "Performance Counter for checking no of files sent", counters);
             }
             try
             {
                 _TotalFiles = new PerformanceCounter();
                 _TotalFiles.CategoryName = "Total_Files";
                 _TotalFiles.CounterName = "Total_Files_Sent";
                 _TotalFiles.MachineName = ".";
                 _TotalFiles.ReadOnly = false;
             }
             catch (Exception e)
             {
                 Console.WriteLine(" {0} 'n", e.StackTrace);
             }
        }

        public void IncrementCounters()
        {
            _TotalFiles.Increment();
        }
    }
}

计数器加1的代码:

    private void BeginSendFilesFinished(IAsyncResult ar) 
    {
        currentOperation.End("BeginSendFilesFinished", () => session.EndUploadFile(ar)); 
        Console.WriteLine(" File Count  : {0} 'n", listoffiles.Count);
        if(sentFiles <  (listoffiles.Count-1))
        {
                sentFiles++;  
                string SampleFile = listoffiles[sentFiles].ToString();
                FileInfo filetoupload = new FileInfo(SampleFile);                        
                ChatRoomFileUploadJob uploadfilejob = new ChatRoomFileUploadJob(filetoupload);
                counterHelper.IncrementCounters();   
                currentOperation.Begin(string.Format("Send Files: # {0}", sentFiles), () => session.BeginUploadFile(uploadfilejob, BeginSendFilesFinished, null));
        }
        else
        {
            Log("Leave ChatRoom:");
            // After sending 10 chat msgs, leave the chat room (and rejoin).
            currentOperation.Begin("Leave ChatRoom:", () => session.BeginLeave(BeginLeaveChatRoomFinished, null));
        }
    }

谢谢,问候,Tazim .

取消性能计数器的注册

这可能太明显了,但是考虑到您正在尝试执行与PerformanceCounterCategory相反的操作。创建动作(我认为),你看过使用PerformanceCounterCategory.Delete吗?根据文档它:

删除类别及其来自本地的关联计数器计算机