Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Variable won't update

Tags:

c#

I'm writing a C# Program to display the Temperature of CPU/GPU from my PS3. There is a connect button. This works really good and it shows me the Temp. from my PS3's CPU/GPU.

Now I've implemented a "refresh" button, which starts a timer for all x Seconds to do this:

public void get_psdata()
        {
            //Get data from PS3
            cputemp = PS3.GetTemperatureCELL();
            gputemp = PS3.GetTemperatureRSX();
            psversion = PS3.GetFirmwareVersion();
            psversiontype = PS3.GetFirmwareType();

            //Set data into Var
            L_cputemp_show.Text = cputemp;
            L_gputemp_show.Text = gputemp;
            L_firmware_show.Text = psversion;
            L_type_show.Text = psversiontype;

            //Update Label
            L_cputemp_show.Refresh();
        }

So this "get_psdata" works only on the first time, when i press the connect button. (The connect button starts directly the "get_psdate" function, while the refresh button does a bit different, like you can see later...)

Here is the code to run the get_psdata:

//B_connect, Connect Button
        private void b_connect_Click(object sender, EventArgs e)
        {
            //Connect CCAPI to PS3 if Button clicked
            PS3.ConnectTarget(psip);

            //Check Connection
            if (PS3.SUCCESS(PS3.ConnectTarget(psip)))
            {
                //Show Status
                MessageBox.Show("Connected to: " + psip + "!");
                this.L_status_show.Text = "Connected!"; L_status_show.ForeColor = System.Drawing.Color.Green;

                //Call Function
                get_psdata();
            }
            else
            {
                //Show Status
                MessageBox.Show("Failed to Connect to: " + psip + "!");
                this.L_status_show.Text = "Not Connected!"; L_status_show.ForeColor = System.Drawing.Color.Red;
            }
        }

For testing, I added a Messagebox.Show to the "get_psdata" function to see if it runs all x Seconds... Yes it does and this is my timer:

        //Function to set refresh delay
        public void refresh_delay()
        {
            MessageBox.Show("Delay set to " + refresh_int + " Seconds!");
            refresh_int = refresh_int * 1000; //Change to Miliseconds
            init_timer();
        }
        //Timer
        public Timer timer1;
        public void init_timer()
        {
            timer1 = new Timer();
            timer1.Tick += new EventHandler(timer1_Tick);
            timer1.Interval = refresh_int; // in miliseconds
            timer1.Start();
        }
        public void timer1_Tick(object sender, EventArgs e)
        {
            get_psdata();
        }

And this is what starts my timer:

    //B_set, Set refresh time button
    private void B_set_Click(object sender, EventArgs e)
    {
        //Check refresh Value
        refresh_string = TB_refresh.Text;

        //Check empty
        if (refresh_string != "")
        {
            //Check minimum
            refresh_int = Convert.ToInt32(TB_refresh.Text);
            if (refresh_int < 5)
            {
                DialogResult confirm = MessageBox.Show("This is not the delay you are looking for! \r (I recommend to set it bigger then 5) \r Continue with " + refresh_int + " Seconds?", "Realy dude?", MessageBoxButtons.YesNo);
                if (confirm == DialogResult.Yes)
                {
                    //Call Function
                    refresh_delay();
                }
            }
            else
            {
                //Call Function
                refresh_delay();
            }
        }
        else
        {
            MessageBox.Show("Please set refresh delay!");
        }
    }

So I'm sure that the code will run all x Seconds but the label's are only updated when I hit the connect button, but not if I start the counter after connecting with the B_set button.

The Variables from "get_psdata" are not showing the updated value. They just show the result from the first "Get". Why aren't they show the latest result?

like image 848
k304 Avatar asked Nov 01 '22 12:11

k304


1 Answers

If I use your code in a fresh winforms screen, all works well. I did however use the following implementation for get_psdata.

public void get_psdata()
{
    var rnd = new Random();

    //Set data into Var
    L_cputemp_show.Text = rnd.Next().ToString();
    L_gputemp_show.Text = rnd.Next().ToString();
    L_firmware_show.Text = rnd.Next().ToString();
    L_type_show.Text = rnd.Next().ToString();

    //Update Label
    L_cputemp_show.Refresh();
}

With an interval of 1 second, this gives me new values on screen every second. Could you try this code please? If this works well, then the problem is with the PS3 object that doesn't refresh its internals perhaps?

like image 161
Tim Eeckhaut Avatar answered Nov 23 '22 11:11

Tim Eeckhaut