Ian McCullough
Ian McCullough

Reputation: 1443

label text not displaying correct value but messagebox.show does

I have a simple label in C#.NET that does not seem to be "updating". It displays only the first initial values of my application. In the following code, the "score" variable does not update however it shows the correct value when the messagebox.show dialog is called. The score value is changed in a different thread, but I don't believe it to be a problem of invokes and cross thread form control (as I am calling this code in the thread that label6 was created on).

Does anyone know what might be a solution? I tried the Application.DoEvents() method with no avail. Also, neither label6.Update() nor label6.Refresh() seem to work when placed after the label6.Text = score line.

Player is a class I created holding the score value as a public int.

    public Form1()
    {
        InitializeComponent();
        createGame();
    }
    public void createGame()
    {
        InitializeComponent();
        drawThread = new Thread(draw);  
        MessageBox.Show(player.score);
        label6.Text = player.score;
    }
    public void draw()
    {
        //do drawing, change player.score value
        //end thread
    }
    public void button_click()
    {
        if(firstrun)
            drawThread.Start()
        else{
            createGame()
            drawThread.Start()
        }

    }

EDIT (from comments): This is a WinForms application. The label6.Text = score line is the ONLY instance where label6 is called other than being instantiated in the Form.Designer code generated by Visual Studio.

Upvotes: 1

Views: 1978

Answers (2)

Habib
Habib

Reputation: 223402

just do Refresh() on the label.

label6.Refresh();

Upvotes: 2

Marco
Marco

Reputation: 57593

In your code I see

drawThread = new Thread(draw);

but on button click you call draw.Start() so I think your rendering thread is not started at all! Shouldn't you use drawThread.Start()?
More: you cannot access graphical controls properties from a thread different from the main, you should use Invoke...

I see a problem here:

  1. drawThread = new Thread(draw);
    MessageBox.Show(player.score);

    With these lines second thread is started and MessageBox is showed immediately (second thread is not terminated yet probably)

  2. label6.Text = player.score;
    When you close MessageBox this line is executed: if second thread is terminated and has already updated score you will get expected result; if second thread is still executing, in your label you still find previous score.

So without knowing what your second thread does, I cannot know if your code is correct or not.
In my opinion you should update label6.Text from your second thread (using Invoke) when it's terminated; if not, why are you using a second thread?
Usually we use threads to perform long executions without blocking main thread (which takes care of updating form and process user input), so you should update properties from these threads when it's needed.
Think about using BackgroundWorker.

Upvotes: 1

Related Questions