C # gets CPU usage for a process (similar to display CPU in Task Manager)

Source: Internet
Author: User

Using System;
Using System.Collections.Generic;
Using System.Diagnostics;
Using System.Linq;
Using System.Management;
Using System.Text;
Using System.Threading;
Using System.Threading.Tasks;

Namespace Process Monitoring
{
Class Program
{
static void Main (string[] args)
{
Process[] processes = Process.getprocessesbyname ("taskmgr");

foreach (Process instance in processes)
{
Console.WriteLine ("");
Console.WriteLine ("ProcessName:" + instance. ProcessName);
Try
{
Console.WriteLine ("Commit size \ t" + instance. pagedmemorysize64/1024);
Console.WriteLine ("Working setup (memory) \ t" + instance. workingset64/1024);
Console.WriteLine ("Number of threads \ t" + instance. Threads.count);
Console.WriteLine ("Number of handles \ t" + instance. Handlecount);

}
Catch {}
}

Process p = processes[1];
PerformanceCounter ramcounter = new PerformanceCounter ("Process", "Working Set", p.processname);
PerformanceCounter cpucounter = new PerformanceCounter ("Process", "% Processor time", p.processname);


var objquery = new ObjectQuery ("SELECT * from Win32_Process WHERE ProcessID =" + p.id);
var mosearcher = new ManagementObjectSearcher (objquery);
DateTime firstsample = datetime.minvalue, secondsample = Datetime.minvalue;

Double processorusage;
Double mspassed;
ULONG u_oldcpu = 0;
while (true)
{
var gets = Mosearcher.get ();
foreach (ManagementObject mobj in gets)
{
Try
{
if (firstsample = = Datetime.minvalue)
{
Firstsample = DateTime.Now;
Mobj.get ();
U_OLDCPU = (ULONG) mobj["Usermodetime"] + (ULONG) mobj["Kernelmodetime"];
}
Else
{
Secondsample = DateTime.Now;
Mobj.get ();
ULONG U_NEWCPU = (ULONG) mobj["Usermodetime"] + (ULONG) mobj["Kernelmodetime"];

mspassed = (secondsample-firstsample). TotalMilliseconds;
Processorusage = (U_NEWCPU-U_OLDCPU)/(mspassed * 100.0 * environment.processorcount);

U_OLDCPU = U_NEWCPU;
Firstsample = Secondsample;
Console.WriteLine ("Processorusage:" + (int) processorusage);
}

}
catch (Exception ex)
{
Console.WriteLine (ex. Message + ex. StackTrace);
Console.WriteLine (ex. Innerexception.message);
}
}
Thread.Sleep (1000);
}
Console.ReadLine ();
}
}
}

C # gets CPU usage for a process (similar to display CPU in Task Manager)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.