Save last value to variable and display

Asked

Viewed 367 times

0

I’m having a problem in my program its function is to display on the screen how much ram memory the process is currently consuming and display the peak of memory usage, so my problem is when I close the process, the program stops displaying the ram peak.

Here the part of the code that displays the ram peak:

        public string vmax()
    {

        System.Diagnostics.Process[] ieProcs = Process.GetProcessesByName(label92.Text);
       double avvv = 0;
       string abi = null;
        try
        {

                if (ieProcs.Length > 0)
                {
                    foreach (System.Diagnostics.Process p in ieProcs)
                    {
                        String physicalMem = p.PeakWorkingSet64.ToString();
                        abi = physicalMem;
                    }
                }

                avvv = double.Parse(abi);
               avvv = avvv * 0.001 / 1024;
            return avvv + " K";


        }
        catch
        {
            return "";
        }
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        label90.Text = vmax();
    }

With the calculator process open:

With the calculator process closed:

inserir a descrição da imagem aqui

I wanted even when I closed the process it would continue displaying the last value recorded by the peak.

1 answer

1


Following past details, follows a change based on what you need. The idea as I understand is to monitor the memory consumption of a process in your application, and record the highest value verified, if the process is finalized your application should keep the highest value recorded so far, correct?

I created a small example form where I enter the name of the process (in my case Calc).

inserir a descrição da imagem aqui

See that on the Pico label I’m showing the highest registered value, this is done by the code I added to your Vmax method:

 if (avvv > valorMax)
   valorMax = avvv;
 lblPico.Text = valorMax + "K";

The above code was added just before the Return. Remembering to declare a variable in the class:

private double valorMax;

As I use the windows calculator(Calc), the "current value" is being modified as well as the Pico (if the current one is bigger than the last recorded peak). Even if I finish the process, that is, close the calculator, the peak value is maintained and as soon as I open the process again the peak will still be there.

Unless you wish to register this even if your application is finalized, I believe it meets your need. To perpetuate this would just save the last peak in an xml or txt, and when the application opens again load the value.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.