Пример #1
0
 public void Start()
 {
     lock (this)
     {
         Stop();
         monitorThread              = new Thread(monitorRunner);
         monitorThread.Name         = "PerfMonManager " + spec.GetLabel();
         monitorThread.IsBackground = true;
         monitorThread.Start();
     }
 }
Пример #2
0
        /// <summary>
        /// Loops until this instance is stopped, gathering performance data at the specified interval.
        /// </summary>
        private void monitorRunner()
        {
            try
            {
                int checkInstanceNameCounter = 0;
                int intervalMs = spec.GetIntervalMs();
                while (true)
                {
                    try
                    {
                        string instanceName = spec.GetInstanceName();
                        if (instanceName == null && spec.processFinder != null)
                        {
                            Logger.Info("Failed to find instance name for monitor " + spec.GetLabel());
                            Thread.Sleep(Math.Max(intervalMs, 10000));
                            continue;
                        }
                        using (PerformanceCounter counter = new PerformanceCounter(spec.categoryName, spec.counterName, instanceName, true))
                        {
                            counter.NextValue();
                            Thread.Sleep(intervalMs);
                            while (true)
                            {
                                if (spec.processFinder != null && ++checkInstanceNameCounter % 15 == 0 && spec.GetInstanceName() != counter.InstanceName)
                                {
                                    break;                                     // Breaking here will cause the PerformanceCounter to be reinitialized.  This will catch process restarts.
                                }
                                PerfMonValue pmv = new PerfMonValue();
                                pmv.Time  = (TimeUtil.GetTimeInMsSinceEpoch() / 100) * 100;                                // Round off to in 100ms interals so the graph tooltip works a little better.
                                pmv.Value = counter.NextValue();

                                lock (this)
                                {
                                    values.AddFirst(pmv);
                                    while (values.Last.Value.Time < pmv.Time - maxAge)
                                    {
                                        values.RemoveLast();
                                    }
                                }

                                Thread.Sleep(intervalMs);
                            }
                        }
                    }
                    catch (ThreadAbortException) { }
                    catch (Exception ex)
                    {
                        Logger.Debug(ex);
                    }
                }
            }
            catch (ThreadAbortException) { }
            catch (Exception ex)
            {
                Logger.Debug(ex);
            }
        }