Esempio n. 1
0
        public void OnTimer(object state)
        {
            try
            {
                if (_firstMove)
                {
                    // some counters need two samples to calculate their value
                    // so skip a sample to make sure there are no further complications
                    PdhNativeMethods.PdhCollectQueryData(_query);
                    _firstMove = false;
                    return;
                }

                long      time;
                PdhStatus status = PdhNativeMethods.PdhCollectQueryDataWithTime(_query, out time);
                PdhUtils.CheckStatus(status, PdhStatus.PDH_CSTATUS_VALID_DATA);
                DateTime timestamp = TimeUtil.FromFileTime(time);

                foreach (PerfCounterInfo counterInfo in _counters)
                {
                    ProduceCounterSamples(counterInfo, timestamp);
                }
            }
            catch (Exception ex)
            {
                _observer.OnError(ex);
            }
        }
        public void Read()
        {
            try
            {
                if (_firstMove)
                {
                    // some counters need two samples to calculate their value
                    // so skip a sample to make sure there are no further complications
                    PdhNativeMethods.PdhCollectQueryData(_query);
                    _firstMove = false;
                }

                while (true)
                {
                    long      time;
                    PdhStatus status = PdhNativeMethods.PdhCollectQueryDataWithTime(_query, out time);
                    if (status == PdhStatus.PDH_NO_MORE_DATA)
                    {
                        break;
                    }

                    if (status == PdhStatus.PDH_NO_DATA)
                    {
                        if (_binaryLog)
                        {
                            continue;
                        }
                        else
                        {
                            break;
                        }
                    }

                    PdhUtils.CheckStatus(status, PdhStatus.PDH_CSTATUS_VALID_DATA);
                    //DateTime timestamp = TimeUtil.FromFileTime(time);
                    DateTime timestamp = DateTime.FromFileTimeUtc(time);

                    foreach (PerfCounterInfo counterInfo in _counters)
                    {
                        ProduceCounterSamples(counterInfo, timestamp);
                    }
                }

                _observer.OnCompleted();
            }
            catch (Exception ex)
            {
                _observer.OnError(ex);
            }
        }