tags:

views:

240

answers:

6

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.

A: 

Try the following:

if(e.KeyChar == '\n')
    SetLabel8Text("Enter key Pressed !");
SLaks
+4  A: 

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.

Jon Skeet
It's certainly possible that the other answers, recommending better implementation of the KeyPress handler, will be helpful. But given that this is intermittent, Jon's advice is likely to be the most useful.
Michael Petrotta
A: 

Try:

if (e.KeyCode == Keys.Enter) {
}
rein
+1  A: 

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).

SLaks
A: 

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.

sindre j
A: 

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.

Qberticus