C#变量不会更新

时间:2014-12-18 21:35:23

标签: c#

我正在编写一个C#程序来显示PS3的CPU / GPU温度。 有一个连接按钮。这非常好,它向我展示了Temp。来自我的PS3的CPU / GPU。

现在我已经实施了#34;刷新"按钮,启动所有x秒的计时器来执行此操作:

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();
        }

所以这个" get_psdata"当我按下连接按钮时,仅在第一次工作。 (连接按钮直接启动" get_psdate"功能,而刷新按钮略有不同,就像你稍后会看到的那样......)

以下是运行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;
            }
        }

为了测试,我在" get_psdata"中添加了一个Messagebox.Show。函数,看它是否全部运行x秒......是的,它是我的计时器:

        //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();
        }

这就是启动我的计时器的原因:

    //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!");
        }
    }

所以我确定代码将运行所有x秒,但标签只会在我点击连接按钮时更新,但是如果我在连接B_set按钮后启动计数器则不会。<\ n / p>

来自&#34; get_psdata&#34;的变量没有显示更新的值。他们只显示第一个&#34; Get&#34;的结果。 为什么他们没有显示最新结果?

1 个答案:

答案 0 :(得分:2)

如果我在新的winforms屏幕中使用您的代码,一切正常。但是,我对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();
}

间隔为1秒,这每秒都会在屏幕上显示新值。你能试试这个代码吗? 如果这样运作良好,那么问题在于PS3对象可能不会刷新其内部结构吗?