3

I am in the process of writing an application that communicates with several devices through GPIB commands, running a test on some equipment. I've set up a class, TestProcedure, which will start a new thread, and run the testing. Throughout testing, I've set up several custom events to send information back to the GUI. Here is an example of a simple event:

    public event InformationEventHandler<string> TestInfoEvent;
    /// <summary>
    /// Event raised when information should be passed back to the main testing form.
    /// </summary>
    /// <param name="s">Information to send back to form.</param>
    private void OnInfo(string s)
    {
        if (TestInfoEvent != null)
            TestInfoEvent(this, s);
    }

Which would be handled through the GUI, updating a text box like this:

TheTestProcedure.TestInfoEvent += new TestProcedure.InformationEventHandler<string> 
                                 (InfoOccurred);
....
private void InfoOccurred(Object sender, string s)
{
    this.textBox1.Text = s + Environment.NewLine + this.textBox1.Text;
    if (this.textBox1.Text.Length > 10000)
        this.textBox1.Text = this.textBox1.Text.Remove(1000);
}

This event handling seems to be working fine. I haven't received any cross threading issues, and overall it's been working as expected. However, on another form I just added a similar event handler, which throws a cross-thread exception. The event fires, sending a simple class with a bit of information that I display in an InputTextBox (A custom ComponentOne control). The particular control does not have a .Invoke method, so I'm looking for alternative solutions to access it asynchronously.

So my question is, are event handlers safe to access controls on a form? If not, how do event handlers fire, and could somebody help educate me, or provide some linked information, as to how an event handler communicates with form controls? Do I need to lock the event?

Corey
  • 398
  • 1
  • 4
  • 18

2 Answers2

2

Controls on the UI thread may only be accessed from the UI thread - any access from other threads is bound to cause issues. You need to use InvokeRequired and BeginInvoke() to marshal an event to the right thread if it's not already there.

Example

Community
  • 1
  • 1
xxbbcc
  • 16,930
  • 5
  • 50
  • 83
  • I should add an invoke to the event itself? Will this work in the same was as say invoking a text box? Edit: Sorry, just saw your example I'll take a look – Corey Feb 29 '12 at 18:05
  • You'd check for `InvokeRequired` first inside your event handler function and if it returns `true`, you need to marshal the call to the UI thread using `BeginInvoke()`. If the return value is `false` you're already on the UI thread so you can safely access the control directly (e.g. `txtName.Text = ...;` is valid then. If you're on the wrong thread, it may fail.) – xxbbcc Feb 29 '12 at 18:07
  • 1
    I fixed the answer to refer to `BeginInvoke()` since you'll likely need that, not `Control.Invoke()`. – xxbbcc Feb 29 '12 at 18:11
  • I just tested, and it's working perfectly. Thanks for the quick response! – Corey Feb 29 '12 at 18:15
1

You'll want to create a delegate callback and Invoke() execute that after testing the InvokeRequired property. The following code will handle the addition in a thread safe manner.

TheTestProcedure.TestInfoEvent += new TestProcedure.InformationEventHandler<string> 
                             (InfoOccurred);

private void InfoOccurred(Object sender, string s)
{
    LogMessage(s);
}

delegate void LogMessageCallback(string text);

void LogMessage(String message)
{
    if (this.textBox1.InvokeRequired)
        this.Invoke(new LogMessageCallback(LogMessage), message);
    else
    {
        this.textBox1.Text = s + Environment.NewLine + this.textBox1.Text;
        if (this.textBox1.Text.Length > 10000)
            this.textBox1.Text = this.textBox1.Text.Remove(1000);
    }
}
Walk
  • 1,136
  • 8
  • 8