Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7400197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:10:38+00:00 2026-05-29T04:10:38+00:00

I would like to retrieve the cpu and ram usage at that point for

  • 0

I would like to retrieve the cpu and ram usage at that point for the foreground process (or a specific one).
Retrieving the window’s title is not a problem and that part works. But the cpu display stays at 0% even when the active window is running at 70% cpu or more.

(int)pCPU.NextValue(); // <<<< keeps returning 0…

Note: I want to do it with a performance counter. I don’t want to do it using the Process variable because that one might raise ‘insufficient privilege errors’.

    [DllImport("user32.dll")]
    static extern IntPtr GetForegroundWindow();

    [DllImport("user32.dll")]
    static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);

    [DllImport("user32.dll")]
    public static extern IntPtr GetWindowThreadProcessId(IntPtr hWnd, out uint ProcessId);

    public void GetActiveCPUAndRam(out string windowTitle, out int CPUUsagePerc, out int RAMUsage)
    {
        IntPtr hwnd = GetForegroundWindow();
        uint pid;
        GetWindowThreadProcessId(hwnd, out pid);

        Process activeProc = Process.GetProcessById((int) pid);

        #region Window Title

        const int nChars = 256;
        StringBuilder Buff = new StringBuilder(nChars);
        if (GetWindowText(hwnd, Buff, nChars) > 0)
            windowTitle = Buff.ToString();
        else
        {
            windowTitle = "";
            CPUUsagePerc = 0;
            RAMUsage = 0;
            return;
        }

        #endregion

        #region RAM/CPU
        PerformanceCounter pCPU = new PerformanceCounter("Process", "% Processor Time", activeProc.ProcessName, true);
        pCPU.NextValue();
        CPUUsagePerc = (int)pCPU.NextValue(); // <<<<< problem here.

        RAMUsage = 0; // TODO:

        #endregion
    }

EDIT:
I tried the new solution:
But when I run a cpu stress test program that pushes the cpu usage to 100% (single core) . Then the solution below shows that the cpu usage of that process is like 300-400% of the total cpu… Obviously something is still wrong.


        private PerformanceCounter pCPU = null;
        private IntPtr PreviousProcHwnd = IntPtr.Zero;
        private CounterSample PreviousCPUCounterSample = CounterSample.Empty;

        public void GetActiveCPUAndRam(out string windowTitle, out int CPUUsagePerc, out int RAMUsage)
{
        ...
        #region RAM/CPU

        if (PreviousProcHwnd != hwnd)
        {
            PreviousProcHwnd = hwnd;
            pCPU = new PerformanceCounter("Process", "% Processor Time", activeProc.ProcessName,
                                                             true);
            PreviousCPUCounterSample = CounterSample.Empty;
        }

        CounterSample sample1 = pCPU.NextSample();
        CPUUsagePerc = (int)CounterSample.Calculate(PreviousCPUCounterSample, sample1);
        PreviousCPUCounterSample = sample1;
        RAMUsage = 0; // TODO:

        #endregion
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T04:10:41+00:00Added an answer on May 29, 2026 at 4:10 am

    Do not use the value directly, use the calculated sample,

    CounterSample sample1 = pCPU.NextSample();
    float value = CounterSample.Calculate(sample1);
    

    if your counter is a ‘rate’ type sample, then you will need to get two samples,

    CounterSample sample1 = counter.NextSample();
    Thread.Sleep(1000); // wait some time
    CounterSample sample2 = counter.NextSample();
    float value = CounterSample.Calculate(sample1, sample2);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to retrieve the ethernet address of the network interface that is
The contents of one TabItem is large CPU expensive to create. Would like to
I would like to retrieve articles that are within 1 year from the today's
I would like to retrieve an error message that explains why the jvm failed
I would like to retrieve the process ID and handle of a process by
I would like to know if it's possible to retrieve the .cs file that
I would like to retrieve and submit data that contains Rich Text to a
I have an array of numbers and would like to retrieve one of the
I would like to retrieve many 'Access' which have one 'Role' in common. It's
I would like to retrieve an enumeration of instantiated classes that implement an interface

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.