예제 #1
0
 public static extern UInt32 PdhGetFormattedCounterValue(
     PdhCounterHandle phCounter,
     PdhFormat dwFormat,
     IntPtr lpdwType,
     out PDH_FMT_COUNTERVALUE pValue);
예제 #2
0
 public static extern UInt32 PdhGetRawCounterValue(
     PdhCounterHandle phCounter,
     IntPtr lpdwType,
     out PDH_FMT_COUNTERVALUE pValue);
예제 #3
0
        public async Task DoRefresh()
        {
            if (!NeedsRefresh)
            {
                return;
            }

            PDH_FMT_COUNTERVALUE value = default(PDH_FMT_COUNTERVALUE);

            uint result = Pdh.PdhCollectQueryData(_query);

            if (result == Pdh.PDH_NO_DATA)
            {
                throw new MissingCountersException(_counters.Keys);
            }
            if (result != 0)
            {
                throw new Win32Exception((int)result);
            }

            bool multiSample = false;
            List <IPerfCounter> missingCounters = new List <IPerfCounter>();

            foreach (KeyValuePair <IPerfCounter, PdhCounterHandle> counterInstance in _counters)
            {
                IPerfCounter     counter = counterInstance.Key;
                PdhCounterHandle handle  = counterInstance.Value;

                result = Pdh.PdhGetFormattedCounterValue(handle, PdhFormat.PDH_FMT_LARGE, IntPtr.Zero, out value);

                if (result == Pdh.PDH_INVALID_DATA && value.CStatus == Pdh.PDH_CSTATUS_INVALID_DATA && !multiSample)
                {
                    multiSample = true;

                    result = Pdh.PdhCollectQueryData(_query);
                    if (result != 0)
                    {
                        throw new Win32Exception((int)result);
                    }

                    result = Pdh.PdhGetFormattedCounterValue(handle, PdhFormat.PDH_FMT_LARGE, IntPtr.Zero, out value);
                }

                if ((result != 0 && value.CStatus == Pdh.PDH_CSTATUS_NO_INSTANCE) ||
                    result == Pdh.PDH_INVALID_HANDLE ||
                    result == Pdh.PDH_CALC_NEGATIVE_VALUE ||
                    result == Pdh.PDH_CALC_NEGATIVE_DENOMINATOR ||
                    (result == Pdh.PDH_INVALID_DATA && value.CStatus == Pdh.PDH_INVALID_DATA))
                {
                    missingCounters.Add(counter);
                    continue;
                }
                else if (result != 0)
                {
                    if (!_counterFinder.CounterExists(counter))
                    {
                        missingCounters.Add(counter);
                        continue;
                    }

                    throw new Win32Exception((int)result);
                }
                else if (value.CStatus != 0)
                {
                    throw new Win32Exception((int)value.CStatus);
                }

                counter.Value = value.longLongValue;
            }

            if (missingCounters.Count > 0)
            {
                throw new MissingCountersException(missingCounters);
            }

            TimeSpan calculationDelta = DateTime.UtcNow - _lastCalculatedTime;

            _lastCalculatedTime = DateTime.UtcNow;
        }