Attilah
Attilah

Reputation: 17930

Why does my code to set label text sometimes not get called properly?

I have a label in a WinForm.in the app, I create a thread for setting the Text property of the label. since the code responsible for setting the Text property of the label is another thread, I wrote it like this :

private void SetLabel8Text(string text)
    {
        try
        {
            if (this.label8.InvokeRequired)
            {
                SetTextCallback d = new SetTextCallback(SetLabel8Text);
                this.Invoke(d, new object[] { text });
            }
            else
            {
                this.label8.Text = text;
            }
        }
        catch (Exception ex)
        {

        }

    }

now, I also handle the KeyPress event like so :

    if (e.KeyChar.ToString() == "\r")
            {
        SetLabel8Text("Enter key Pressed !");
    }

the problem I'm facing is that after pressing the Enter Key (execution of the KeyPress event), the SetLabel8Text method never gets executed.

everything else seems to flow nicely , I tried stepping through the code and it hangs at this place(inside the SetLabe8Text method :

this.Invoke(d, new object[] { text });

it hangs and doesn't move forward a bit.

Upvotes: 0

Views: 455

Answers (6)

Rich Schuler
Rich Schuler

Reputation: 41972

You're not following the basic tenet of winforms: Only create UI controls on the UI thread. Your event handler for the KeyPress event is on the UI thread, therefore, if your label was created on the UI thread you wouldn't need to use BeginInvoke/Invoke on it.

If you are creating forms, controls, etc. in threads other than the UI thread you're probably doing something wrong.

See: http://weblogs.asp.net/justin_rogers/pages/126345.aspx for the gory details.

Upvotes: 0

sindre j
sindre j

Reputation: 4444

One possible explanation could be what you're doing next. Assuming the SetLabel8Text function is called in another thread it will require invoke, and thus execute once more. However, the second time it's executed (and thus not requiring invoking) it's executing in the thread that owns the GUI, normally the main thread. So if you have code blocking the main thread for some time somewhere else in your application it would seem like the SetLabel8Text function never got executed the second time. Always thread heavy tasks and keep the main thread idle for simplicity.

Upvotes: 0

SLaks
SLaks

Reputation: 887453

Try calling BeginInvoke instead of Invoke.

Invoke is a blocking call, so it's possible that you have a race condition. (Invoke won't return until the method actually gets executed, but the method can only get executed once the UI thread processes its message loop).

Upvotes: 1

rein
rein

Reputation: 33455

Try:

if (e.KeyCode == Keys.Enter) {
}

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1500605

Well the fact that you're swallowing any exceptions thrown by SetLabel8Text makes it hard to know exactly what's going on. You should never swallow exceptions unconditionally without at least logging what's going on. (You also shouldn't just catch "Exception". Catch a more specific type of exception). Could you post a short but complete program which demonstrates the problem.

Adding logging in the key press event and the SetLabel8Text would also help.

Upvotes: 4

SLaks
SLaks

Reputation: 887453

Try the following:

if(e.KeyChar == '\n')
    SetLabel8Text("Enter key Pressed !");

Upvotes: 0

Related Questions