protected override bool ReleaseHandle() { return(PdhApi.PdhRemoveCounter(handle) == 0); }
protected override bool ReleaseHandle() { return(PdhApi.PdhCloseLog(handle, PdhApi.PDH_FLAGS_CLOSE_QUERY) == 0); }
protected override bool ReleaseHandle() { return(PdhApi.PdhCloseQuery(handle) == 0); }
/// <summary> /// gets specified performance counter from the first instance of the WMI class Gpu /// </summary> /// <param name="strCounterName">Name of a Gpu counter</param> public void getGpuCounter(string strCounterName) { try { string className = "Gpu"; string strGpuCounter = ""; // Performs WMI object query in selected namespace string wmiScope = target + nvWmiNamespace; ManagementScope scope = new ManagementScope(wmiScope); WqlObjectQuery wqlQuery = new WqlObjectQuery("select * from " + className); scope.Connect(); ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, wqlQuery, null); ManagementClass classInstance = new ManagementClass(target + nvWmiNamespace, className, null); foreach (ManagementObject gpu in searcher.Get()) { // printing out some properties gpu.Get(); // bind WMI object to an instance of the .NET ManagementObject uint id = (uint)gpu["id"]; // GPU ID uint nvapiId = (uint)gpu["nvapiId"]; // GPU NVAPI ID string name = (string)gpu["name"]; // GPU name strGpuCounter = string.Format(@"\NVIDIA GPU(#{0:d} {1} (id={2:d}, NVAPI ID={3:d}))\{4}", id - 1, name, id, nvapiId, strCounterName); break; // bail out with the perf counter for the 1st GPU in a system } PdhQueryHandle query; PdhCounterHandle counter; // result is PDH_STATUS in C++ UInt32 result = PdhApi.PdhOpenQuery(null, IntPtr.Zero, out query); if (result != 0) { Console.WriteLine("error opening performance query, status={0:d} (0x{0:X8})", result); return; } // Add the counter that we want to see // The PdhEnumXXX methods can be used to discover them result = PdhApi.PdhAddCounter(query, strGpuCounter, IntPtr.Zero, out counter); if (result != 0) { // 0xC0000BCD (PDH_ENTRY_NOT_IN_LOG_FILE) means the counter was not found in the set, // happens when counter name is mistyped or non-localized string used in localized OS Console.WriteLine("error adding counter, status={0:d} (0x{0:X8})", result); return; } do { result = PdhApi.PdhCollectQueryData(query); if (result == 0) { PDH_FMT_COUNTERVALUE value; result = PdhApi.PdhGetFormattedCounterValue(counter, PdhFormat.PDH_FMT_DOUBLE, IntPtr.Zero, out value); if (value.CStatus == 0) { Console.WriteLine("temperature = {0:g}", value.doubleValue); } else { // 0xC0000BC6 (PDH_INVALID_DATA) can be returned when more samples are needed to calculate the value. // Report error and continue calling PdhCollectQueryData Console.WriteLine("error retrieving counter value, status={0:d} (0x{0:X8})", value.CStatus); } } System.Threading.Thread.Sleep(1000); }while (result == 0 && !Console.KeyAvailable); } catch (ManagementException e) { Console.WriteLine("Exception caught: " + e.Message); Console.WriteLine("Stack trace: " + e.StackTrace); } }