Example #1
0
        private void GetCurrentCpuUsage(object data)
        {
            bool      monitor  = ((ProcessInfo)data).monitor;
            Process   process  = ((ProcessInfo)data).process;
            ArrayList cpuUsage = ((ProcessInfo)data).cpuUsage;

            try
            {
                ProcessPerformanceCounter perf = new ProcessPerformanceCounter(
                    common, process.ProcessName, process.Id);
                double value = perf.NextValue();
                value = Math.Round(value, 2, MidpointRounding.AwayFromZero);
                common.LogMessage("value: " + value + " : " + process.Id);
                if (monitor)
                {
                    common.LogProcessStat(string.Format("CPUINFO-{0}({1}) - {2}",
                                                        process.ProcessName, process.Id, value));
                }
                cpuUsage.Add(value);
            }
            catch (Exception ex)
            {
                common.LogMessage(ex);
            }
        }
Example #2
0
 public double[] GetCpuUsage(string processName, bool monitor = false)
 {
     ArrayList cpuUsage = new ArrayList();
     ArrayList threadPool = new ArrayList();
     try
     {
         processName = GetProcessName(processName);
         common.LogMessage(processName);
         Process[] processes = Process.GetProcessesByName(processName);
         foreach (Process p in processes)
         {
             try
             {
                 // Need to call this once, before calling GetCurrentCpuUsage
                 ProcessPerformanceCounter perf = new ProcessPerformanceCounter(
                     common, processName, p.Id);
                 if (perf != null)
                     perf = null;
                 ProcessInfo processInfo = new ProcessInfo();
                 processInfo.process = p;
                 processInfo.monitor = monitor;
                 processInfo.cpuUsage = cpuUsage;
                 if (processes.Length > 1)
                 {
                     // Increased the performance of output
                     // else for running 10+ instance of an app
                     // takes lot of time
                     Thread thread = new Thread(new ParameterizedThreadStart(
                         GetCurrentCpuUsage));
                     thread.Start(processInfo);
                     threadPool.Add(thread);
                 }
                 else
                     GetCurrentCpuUsage(processInfo);
             }
             catch (Exception ex)
             {
                 common.LogMessage(ex);
             }
         }
         foreach (Thread thread in threadPool)
         {
             // Wait for each thread to complete its job
             thread.Join();
         }
         if (common.Debug)
         {
             foreach (double value in cpuUsage)
                 common.LogMessage(value);
         }
         return cpuUsage.ToArray(typeof(double)) as double[];
     }
     catch (Exception ex)
     {
         common.LogMessage(ex);
         return cpuUsage.ToArray(typeof(double)) as double[];
     }
     finally
     {
         cpuUsage = null;
         threadPool = null;
     }
 }
Example #3
0
 private void GetCurrentCpuUsage(object data)
 {
     bool monitor = ((ProcessInfo)data).monitor;
     Process process = ((ProcessInfo)data).process;
     ArrayList cpuUsage = ((ProcessInfo)data).cpuUsage;
     try
     {
         ProcessPerformanceCounter perf = new ProcessPerformanceCounter(
                     common, process.ProcessName, process.Id);
         double value = perf.NextValue();
         value = Math.Round(value, 2, MidpointRounding.AwayFromZero);
         common.LogMessage("value: " + value + " : " + process.Id);
         if (monitor)
             common.LogProcessStat(string.Format("CPUINFO-{0}({1}) - {2}",
                 process.ProcessName, process.Id, value));
         cpuUsage.Add(value);
     }
     catch (Exception ex)
     {
         common.LogMessage(ex);
     }
 }
Example #4
0
        public double[] GetCpuUsage(string processName, bool monitor = false)
        {
            ArrayList cpuUsage   = new ArrayList();
            ArrayList threadPool = new ArrayList();

            try
            {
                processName = GetProcessName(processName);
                common.LogMessage(processName);
                Process[] processes = Process.GetProcessesByName(processName);
                foreach (Process p in processes)
                {
                    try
                    {
                        ProcessPerformanceCounter perf = new ProcessPerformanceCounter(
                            common, processName, p.Id);
                        ProcessInfo processInfo = new ProcessInfo();
                        processInfo.process  = p;
                        processInfo.monitor  = monitor;
                        processInfo.cpuUsage = cpuUsage;
                        if (processes.Length > 1)
                        {
                            // Increased the performance of output
                            // else for running 10+ instance of an app
                            // takes lot of time
                            Thread thread = new Thread(new ParameterizedThreadStart(
                                                           GetCurrentCpuUsage));
                            thread.Start(processInfo);
                            threadPool.Add(thread);
                        }
                        else
                        {
                            GetCurrentCpuUsage(processInfo);
                        }
                    }
                    catch (Exception ex)
                    {
                        common.LogMessage(ex);
                    }
                }
                foreach (Thread thread in threadPool)
                {
                    // Wait for each thread to complete its job
                    thread.Join();
                }
                if (common.Debug)
                {
                    foreach (double value in cpuUsage)
                    {
                        common.LogMessage(value);
                    }
                }
                return(cpuUsage.ToArray(typeof(double)) as double[]);
            }
            catch (Exception ex)
            {
                common.LogMessage(ex);
                return(cpuUsage.ToArray(typeof(double)) as double[]);
            }
            finally
            {
                cpuUsage   = null;
                threadPool = null;
            }
        }