0

I have a process

Process p = new Process();
p.StartInfo = new ProcessStartInfo("java", -jar test.jar);

and I need get RAM, CPU of process p is used in realtime

my code to get RAM is used but not in realtime:

while(true) {
    txtRamIsUsed.Text = BytesToReadableValue(p.PrivateMemorySize64);
}
public string BytesToReadableValue(long number)
{
    List<string> suffixes = new List<string> { " B", " KB", " MB", " GB", " TB", " PB" };

    for (int i = 0; i < suffixes.Count; i++)
    {
        long temp = number / (int)Math.Pow(1024, i + 1);

        if (temp == 0)
        {
            return (number / (int)Math.Pow(1024, i)) + suffixes[i];
        }
    }

    return number.ToString();
}
ASh
  • 34,632
  • 9
  • 60
  • 82
KienKon
  • 13
  • 3
  • What do you mean "realtime"? Continuously display it? – Louis Go Mar 12 '20 at 04:02
  • It is constantly change in realtime – KienKon Mar 12 '20 at 04:15
  • 1
    Use timer control to display it in some small duration. – Atk Mar 12 '20 at 04:33
  • 1
    Use a Timer, say every second, to update the label. Loops can’t be used for updates in Winforms and most other UIs because the event loop can’t process. This will also prevent wasting CPU in a hot-busy loop. There are many duplicates. – user2864740 Mar 12 '20 at 04:37
  • And How to get CPU of ` process p` ? – KienKon Mar 12 '20 at 05:07
  • https://stackoverflow.com/questions/9115436/performance-counter-by-process-id-instead-of-name – TheGeneral Mar 12 '20 at 05:42
  • "realtime" is a hollow term without stating the specifications. There cannot be actual "realtime" on a non-realtime OS, anyway. So, what you actually mean is "update as tightly as possible", right? – Fildor Mar 12 '20 at 08:53

0 Answers0