コード例 #1
0
        /// <summary>
        ///     Removes the counter (category) from the system
        /// </summary>
        public static void Delete(string categoryName)
        {
            CheckValidCategory(categoryName);
            string machineName = ".";

            categoryName = categoryName.ToLowerInvariant();

            Mutex mutex = null;

            try
            {
                NetFrameworkUtils.EnterMutex(PerfMutexName, ref mutex);
                if (!PerformanceCounterLib.IsCustomCategory(machineName, categoryName))
                {
                    throw new InvalidOperationException(SR.CantDeleteCategory);
                }

                SharedPerformanceCounter.RemoveAllInstances(categoryName);

                PerformanceCounterLib.UnregisterCategory(categoryName);
                PerformanceCounterLib.CloseAllLibraries();
            }
            finally
            {
                if (mutex != null)
                {
                    mutex.ReleaseMutex();
                    mutex.Close();
                }
            }
        }
コード例 #2
0
        private static void LoadPerfCounterDll()
        {
            if (s_perfCounterDllLoaded)
            {
                return;
            }

            string installPath = NetFrameworkUtils.GetLatestBuildDllDirectory(".");

            string perfcounterPath = Path.Combine(installPath, "perfcounter.dll");

            if (Interop.Kernel32.LoadLibrary(perfcounterPath) == IntPtr.Zero)
            {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }

            s_perfCounterDllLoaded = true;
        }
コード例 #3
0
        public static PerformanceCounterCategory Create(string categoryName, string categoryHelp, PerformanceCounterCategoryType categoryType, CounterCreationDataCollection counterData)
        {
            if (categoryType < PerformanceCounterCategoryType.Unknown || categoryType > PerformanceCounterCategoryType.MultiInstance)
            {
                throw new ArgumentOutOfRangeException(nameof(categoryType));
            }
            if (counterData == null)
            {
                throw new ArgumentNullException(nameof(counterData));
            }

            CheckValidCategory(categoryName);
            if (categoryHelp != null)
            {
                // null categoryHelp is a valid option - it gets set to "Help Not Available" later on.
                CheckValidHelp(categoryHelp);
            }
            string machineName = ".";

            Mutex mutex = null;

            try
            {
                NetFrameworkUtils.EnterMutex(PerfMutexName, ref mutex);
                if (PerformanceCounterLib.IsCustomCategory(machineName, categoryName) || PerformanceCounterLib.CategoryExists(machineName, categoryName))
                {
                    throw new InvalidOperationException(SR.Format(SR.PerformanceCategoryExists, categoryName));
                }

                CheckValidCounterLayout(counterData);
                PerformanceCounterLib.RegisterCategory(categoryName, categoryType, categoryHelp, counterData);
                return(new PerformanceCounterCategory(categoryName, machineName));
            }
            finally
            {
                if (mutex != null)
                {
                    mutex.ReleaseMutex();
                    mutex.Close();
                }
            }
        }