Beispiel #1
0
        private void DoWork()
        {
            double cpuUsage = 0;
            var    errMsg   = string.Empty;

            while (IsWorking)
            {
                //暂停
                if (IsWorkPause)
                {
                    Thread.Sleep(1);
                    continue;
                }

                //CPU使用率
                if (!_appInfo.GetCpuPerformance(ref cpuUsage, ref errMsg))
                {
                    ErrorAction?.Invoke(errMsg);
                    IsWorking = false;
                    continue;
                }

                //APP内存
                long memAppPrivate    = 0;
                long memAppWorkingSet = 0;
                if (!_appInfo.GetMemInfo(ref memAppPrivate, ref memAppWorkingSet, ref errMsg))
                {
                    ErrorAction?.Invoke(errMsg);
                    IsWorking = false;
                    continue;
                }

                //线程数
                var threadCount = 0;
                if (!_appInfo.GetThreadCount(ref threadCount, ref errMsg))
                {
                    ErrorAction?.Invoke(errMsg);
                    IsWorking = false;
                    continue;
                }

                var appPerformance = new AppPerformanceInfo()
                {
                    SystemMemoryInfo    = GetSysMemInfo(),
                    CpuUsage            = cpuUsage,
                    AppPrivateMemory    = memAppPrivate,
                    AppWorkingSetMemory = memAppWorkingSet,
                    ThreadCount         = threadCount
                };
                ShowInfoAction?.Invoke(appPerformance);

                //等待计时
                var interval = _config.TimerInterval * 1000;
                var count    = interval / 100;
                for (var i = 0; i < count; i++)
                {
                    if (!IsWorking)
                    {
                        return;
                    }

                    Thread.Sleep(100);
                }

                if (interval % 100 > 0)
                {
                    Thread.Sleep(interval % 100);
                }

                Thread.Sleep(1);
            }
        }