Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get CPU usage and/or RAM usage of a *THREAD* in C# (managed code)?

I know how to get CPU usage and memory usage for a process, but I was wondering how to get it on a per-thread level. If the best solution is to do some P-Invoking, then that's fine too.

Example of what I need:

Thread myThread = Thread.CurrentThread;

// some time later in some other function...

Console.WriteLine(GetThreadSpecificCpuUsage(myThread));
like image 374
Timothy Khouri Avatar asked Jun 01 '09 11:06

Timothy Khouri


People also ask

How do I check my CPU and RAM usage?

Press CTRL + Shift + Esc to open Task Manager. Click the Performance tab. This tab displays your system's RAM, CPU, GPU, and disk usage, along with network info. To view RAM usage, select the Memory box.

What is CPU RAM usage?

CPU usage or utilization refers to the time taken by a computer to process some information. RAM usage or MAIN MEMORY UTILIZATION on the other hand refers to the amount of time RAM is used by a certain system at a particular time.


1 Answers

As said, memory use cannot be answered since that is an attribute of the process as a whole, but CPU use:

Process p = Process.GetCurrentProcess(); // getting current running process of the app
foreach (ProcessThread pt in p.Threads)
{
    // use pt.Id / pt.TotalProcessorTime / pt.UserProcessorTime / pt.PrivilegedProcessorTime
}
like image 91
jerryjvl Avatar answered Oct 24 '22 16:10

jerryjvl