none
Cross-thread operation not valid:

    Question

  •  I have created a Windows Forms application using the C# 2008 compiler. I dragged the "SerialPort tool" onto my form so that I could get and send serial communications on the RS-232 port. This works, but when I try to update a textbox on the form with the serial port data I get a "Cross-thread operation not valid: Control 'TextBox' accessed from a thread other than the thread it was created on." The line TextBox.Text = Buffer; below generates the exception. How can I fix this problem?

    private void SerialPort_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
    {
        Buffer = SerialPort.ReadExisting();
        TextBox.Text =Buffer;
    }

    Thanks in advance for any advice.
    Chuck

    Thursday, October 09, 2008 6:54 PM

Answers

  • You can only change properties on UI elements from the main UI thread.  To fix this, you'll need to create some kind of helper method that will redirect the text value you want to be placed in your textbox to the main thread, where the property can be set.  This will require the use of the InvokeRequired property and the Invoke method.  You'll use them like this.

    1. Create a method that sets the text on the textbox.  Place this method in your form code.

    public void SetText(string value)  
    {  
        TextBox.Text = Buffer;  

    2. Create a delegate for this method and place it in the form code.

    delegate void SetTextDelegate(string value); 

    3. Alter this method you created.  You want to check to see if InvokeRequired is true.  If InvokeRequired is true, then you'll have to call Invoke.  Basically, InvokeRequired returns true if the code is running on the UI thread, and false if it is not.  The Invoke method will redirect a method call to a particular method to the UI thread.

    Your method should now look like this:

    public void SetText(string value)  
    {  
        if (InvokeRequired)  
            Invoke(new SetTextDelegate(SetText), value);  
        else 
            TextBox.Text = value;  

    4. Now call this method from your method to set the text:

    private void SerialPort_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)  
    {  
        Buffer = SerialPort.ReadExisting();  
        SetText(Buffer);  


    That should fix the issue for you.
    David Morton - http://blog.davemorton.net/
    Thursday, October 09, 2008 7:08 PM
    Moderator

All replies

  • You can only change properties on UI elements from the main UI thread.  To fix this, you'll need to create some kind of helper method that will redirect the text value you want to be placed in your textbox to the main thread, where the property can be set.  This will require the use of the InvokeRequired property and the Invoke method.  You'll use them like this.

    1. Create a method that sets the text on the textbox.  Place this method in your form code.

    public void SetText(string value)  
    {  
        TextBox.Text = Buffer;  

    2. Create a delegate for this method and place it in the form code.

    delegate void SetTextDelegate(string value); 

    3. Alter this method you created.  You want to check to see if InvokeRequired is true.  If InvokeRequired is true, then you'll have to call Invoke.  Basically, InvokeRequired returns true if the code is running on the UI thread, and false if it is not.  The Invoke method will redirect a method call to a particular method to the UI thread.

    Your method should now look like this:

    public void SetText(string value)  
    {  
        if (InvokeRequired)  
            Invoke(new SetTextDelegate(SetText), value);  
        else 
            TextBox.Text = value;  

    4. Now call this method from your method to set the text:

    private void SerialPort_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)  
    {  
        Buffer = SerialPort.ReadExisting();  
        SetText(Buffer);  


    That should fix the issue for you.
    David Morton - http://blog.davemorton.net/
    Thursday, October 09, 2008 7:08 PM
    Moderator
  • David,

    That works great.  Thanks a million for your help!

    Chuck

    Thursday, October 09, 2008 8:30 PM
  • Great! Dude... works nice;

    Saturday, August 04, 2012 10:23 PM
  • Thank you David. The information that you provided is really helpful to my project. 
    • Edited by Sowri Rekha Wednesday, September 12, 2012 11:29 PM
    Wednesday, September 12, 2012 11:29 PM
  • David, 

    Could you provide me how to stop thread smoothly in serial port communication. Thank you in advance. 

    Wednesday, December 19, 2012 6:49 PM
  • David, 

    Could you provide me how to stop thread smoothly in serial port communication. Thank you in advance. 

    Ask your questions in a new question, not on a 5 year old thread that's not even related.
    Wednesday, December 19, 2012 7:32 PM