<p>I have a <a href="http://www.samurize.com/modules/news/" rel="noreferrer">Samurize</a> config that shows a CPU usage graph similar to Task manager.  </p>

<p>How do I also display the name of the process with the current highest CPU usage percentage?  </p>

<p>I would like this to be updated, at most, once per second.  Samurize can call a command line tool and display it's output on screen, so this could also be an option.</p>

<hr>

<p>Further clarification:  </p>

<p>I have investigated writing my own command line c# .NET application to enumerate the array returned from System.Diagnostics.Process.GetProcesses(), but the Process instance class does not seem to include a CPU percentage property.  </p>

<p>Can I calculate this in some way?</p>

[{'answer_id': 51705, 'author': 'hasseg', 'author_id': 4111, 'author_profile': 'https://Stackoverflow.com/users/4111', 'pm_score': 1, 'selected': False, 'text': '<p>You might be able to use <a href="http://commandwindows.com/server2003tools.htm" rel="nofollow noreferrer">Pmon.exe</a> for this. You can get it as part of the <a href="http://www.microsoft.com/downloads/details.aspx?familyid=9d467a69-57ff-4ae7-96ee-b18c4790cffd" rel="nofollow noreferrer">Windows Resource Kit tools</a> (the link is to the Server 2003 version, which can apparently be used in XP as well).</p>\n'}, {'answer_id': 51743, 'author': 'Ed Guiness', 'author_id': 4200, 'author_profile': 'https://Stackoverflow.com/users/4200', 'pm_score': 1, 'selected': False, 'text': '<pre><code>Process.TotalProcessorTime\n</code></pre>\n\n<p><a href="http://msdn.microsoft.com/en-us/library/system.diagnostics.process.totalprocessortime.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/system.diagnostics.process.totalprocessortime.aspx</a></p>\n'}, {'answer_id': 51789, 'author': 'PabloG', 'author_id': 394, 'author_profile': 'https://Stackoverflow.com/users/394', 'pm_score': -1, 'selected': True, 'text': '<p>With PowerShell:</p>\n\n<pre><code>Get-Process | Sort-Object CPU -desc | Select-Object -first 3 | Format-Table CPU,ProcessName -hidetableheader\n</code></pre>\n\n<p>returns somewhat like:</p>\n\n<pre><code>  16.8641632 System\n   12.548072 csrss\n  11.9892168 powershell\n</code></pre>\n'}, {'answer_id': 51820, 'author': 'Jorge Córdoba', 'author_id': 2695, 'author_profile': 'https://Stackoverflow.com/users/2695', 'pm_score': 3, 'selected': False, 'text': '<p>What you want to get its the instant CPU usage (kind of)...</p>\n\n<p>Actually, the instant CPU usage for a process does not exists. Instead you have to make two measurements and calculate the average CPU usage, the formula is quite simple:</p>\n\n<blockquote>\n  <p>AvgCpuUsed = [TotalCPUTime(process,time2) - TotalCPUTime(process,time1)] / [time2-time1]</p>\n</blockquote>\n\n<p>The lower Time2 and Time1 difference is, the more "instant" your measurement will be. Windows Task Manager calculate the CPU use with an interval of one second. I\'ve found that is more than enough and you might even consider doing it in 5 seconds intervals cause the act of measuring itself takes up CPU cycles...</p>\n\n<p>So, first, to get the average CPU time</p>\n\n<pre><code>    using System.Diagnostics;\n\nfloat GetAverageCPULoad(int procID, DateTme from, DateTime, to)\n{\n  // For the current process\n  //Process proc = Process.GetCurrentProcess();\n  // Or for any other process given its id\n  Process proc = Process.GetProcessById(procID);\n  System.TimeSpan lifeInterval = (to - from);\n  // Get the CPU use\n  float CPULoad = (proc.TotalProcessorTime.TotalMilliseconds / lifeInterval.TotalMilliseconds) * 100;\n  // You need to take the number of present cores into account\n  return CPULoad / System.Environment.ProcessorCount;\n}\n</code></pre>\n\n<p>now, for the "instant" CPU load you\'ll need an specialized class:</p>\n\n<pre><code> class ProcLoad\n{\n  // Last time you checked for a process\n  public Dictionary&lt;int, DateTime&gt; lastCheckedDict = new Dictionary&lt;int, DateTime&gt;();\n\n  public float GetCPULoad(int procID)\n  {\n    if (lastCheckedDict.ContainsKey(procID))\n    {\n      DateTime last = lastCheckedDict[procID];\n      lastCheckedDict[procID] = DateTime.Now;\n      return GetAverageCPULoad(procID, last, lastCheckedDict[procID]);\n    }\n    else\n    {\n      lastCheckedDict.Add(procID, DateTime.Now);\n      return 0;\n    }\n  }\n}\n</code></pre>\n\n<p>You should call that class from a timer (or whatever interval method you like) for <strong>each process you want to monitor</strong>, if you want all the processes just use the <a href="http://msdn.microsoft.com/en-us/library/1f3ys1f9.aspx" rel="nofollow noreferrer">Process.GetProcesses</a> static method</p>\n'}, {'answer_id': 15454096, 'author': 'MD. Mohiuddin Ahmed', 'author_id': 1895925, 'author_profile': 'https://Stackoverflow.com/users/1895925', 'pm_score': 0, 'selected': False, 'text': '<p>You can also do it  this way :-</p>\n\n<pre><code>public Process getProcessWithMaxCPUUsage()\n    {\n        const int delay = 500;\n        Process[] processes = Process.GetProcesses();\n\n        var counters = new List&lt;PerformanceCounter&gt;();\n\n        foreach (Process process in processes)\n        {\n            var counter = new PerformanceCounter("Process", "% Processor Time", process.ProcessName);\n            counter.NextValue();\n            counters.Add(counter);\n        }\n        System.Threading.Thread.Sleep(delay);\n        //You must wait(ms) to ensure that the current\n        //application process does not have MAX CPU\n        int mxproc = -1;\n        double mxcpu = double.MinValue, tmpcpu;\n        for (int ik = 0; ik &lt; counters.Count; ik++)\n        {\n            tmpcpu = Math.Round(counters[ik].NextValue(), 1);\n            if (tmpcpu &gt; mxcpu)\n            {\n                mxcpu = tmpcpu;\n                mxproc = ik;\n            }\n\n        }\n        return processes[mxproc];\n    }\n</code></pre>\n\n<p>Usage:-</p>\n\n<pre><code>static void Main()\n    {\n        Process mxp=getProcessWithMaxCPUUsage();\n        Console.WriteLine(mxp.ProcessName);\n    }\n</code></pre>\n'}, {'answer_id': 19474434, 'author': 'Ayan Mullick', 'author_id': 2748772, 'author_profile': 'https://Stackoverflow.com/users/2748772', 'pm_score': 1, 'selected': False, 'text': "<p>Somehow </p>\n\n<p><code>Get-Process | Sort-Object CPU -desc | Select-Object -first 3 | Format-Table CPU,ProcessName,TotalProcessorTime -hidetableheader</code></p>\n\n<p>wasn't getting the CPU information from the remote machine. I had to come up with this.</p>\n\n<p><code>Get-Counter '\\Process(*)\\% Processor Time' | Select-Object -ExpandProperty countersamples | Select-Object -Property instancename, cookedvalue| Sort-Object -Property cookedvalue -Descending| Select-Object -First 10| ft -AutoSize</code></p>\n"}, {'answer_id': 22675167, 'author': 'Ambrose Leung', 'author_id': 2205372, 'author_profile': 'https://Stackoverflow.com/users/2205372', 'pm_score': 1, 'selected': False, 'text': "<p>Thanks for the formula, Jorge.  I don't quite understand why you have to divide by the number of cores, but the numbers I get match the Task Manager. Here's my powershell code:</p>\n\n<pre><code>$procID = 4321\n\n$time1 = Get-Date\n$cpuTime1 = Get-Process -Id $procID | Select -Property CPU\n\nStart-Sleep -s 2\n\n$time2 = Get-Date\n$cpuTime2 = Get-Process -Id $procID | Select -Property CPU\n\n$avgCPUUtil = ($cpuTime2.CPU - $cpuTime1.CPU)/($time2-$time1).TotalSeconds *100 / [System.Environment]::ProcessorCount\n</code></pre>\n"}, {'answer_id': 31356645, 'author': 'Vidar', 'author_id': 346645, 'author_profile': 'https://Stackoverflow.com/users/346645', 'pm_score': 2, 'selected': False, 'text': '<p>Building on Frederic\'s answer and utilizing the code at the bottom of the page <a href="http://blogs.msdn.com/b/powershell/archive/2012/07/13/join-object.aspx" rel="nofollow">here</a> (for an example of usage see <a href="http://blogs.msdn.com/b/powershell/archive/2012/10/04/joining-multiple-tables-grouping-and-evaluating-totals.aspx" rel="nofollow">this</a> post) to join the full set of processes gotten from <code>Get-Process</code>, we get the following:</p>\n\n<pre><code>$sampleInterval = 3\n\n$process1 = Get-Process |select Name,Id, @{Name="Sample1CPU"; Expression = {$_.CPU}}\n\nStart-Sleep -Seconds $sampleInterval\n\n$process2 = Get-Process | select Id, @{Name="Sample2CPU"; Expression = {$_.CPU}}\n\n$samples = Join-Object -Left $process1 -Right $process2 -LeftProperties Name,Sample1CPU -RightProperties Sample2CPU -Where {$args[0].Id -eq $args[1].Id}\n\n$samples | select Name,@{Name="CPU Usage";Expression = {($_.Sample2CPU-$_.Sample1CPU)/$sampleInterval * 100}} | sort -Property "CPU Usage" -Descending | select -First 10 | ft -AutoSize\n</code></pre>\n\n<p>Which gives an example output of </p>\n\n<pre><code>Name                  CPU Usage\n----                  ---------\nfirefox        20.8333333333333\npowershell_ise 5.72916666666667\nBattle.net               1.5625\nSkype                    1.5625\nchrome                   1.5625\nchrome         1.04166666666667\nchrome         1.04166666666667\nchrome         1.04166666666667\nchrome         1.04166666666667\nLCore          1.04166666666667\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/51684', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/5023/']