0

I have DataReceviedHandler, that is reading the data from the Rx terminal. I will like to change the status of the radioButton after getting Data at Rx (from the DataReceviedHandler) but I'm getting this error: "Cross-thread operation not valid: Control 'radioButton3' accessed from a thread other than the thread it was created on." what should i do to resolve?

Thanks, Oz

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        m_ser.GetType();
        m_ser.PortName = "COM28";
        m_ser.BaudRate = 115200;
        m_ser.Open();
        m_ser.ReadTimeout = 500;
        m_ser.WriteTimeout = 500;

    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

     public void DataReceviedHandler(object sender, SerialDataReceivedEventArgs e)
    {
        byte[] m_rxUSBbuff = new byte[500];
        SerialPort sptemp = (SerialPort)sender;
        UInt16 rxbuffLength = (UInt16)m_ser.BytesToRead;
        byte[] rxBuff = new byte[rxbuffLength];
        try
        {
            rxbuffLength = (UInt16)sptemp.Read(rxBuff, 0, sptemp.BytesToRead);
        }
        catch (System.IO.IOException)
        {
            Console.WriteLine("Faild to Read from serial InputBuffer");
            return;
        }
        int i;
        int m_rxUSBLstMsgLength;
        m_rxUSBLstMsgLength = (UInt16)rxbuffLength;
        for (i = 0; i < m_rxUSBLstMsgLength; i++)
        {
            m_rxUSBbuff[i] = rxBuff[i];
        }
        switch (m_rxUSBbuff[0])
        {
            case 1:
                radioButton1.Checked = true;
                break;
            case 2:
                radioButton2.Checked = true;
                break;
            case 4:
                radioButton3.Checked = true;
                break;
            case 8:
                radioButton4.Checked = true;
                break;
            case 16:
                radioButton5.Checked = true;
                break;
            default:
                break;
        }


    }

    private void CheckLeds_Click(object sender, EventArgs e)
    {
            byte[] txb = new byte[1] { 0xf2 };
            m_ser.Write(txb, 0, 1);
    }


}
oz zoaretz
  • 29
  • 3
  • 1
    from what you describe you seem to have multiple threads - please show relevant source code... – Yahia Apr 02 '12 at 09:15

2 Answers2

1

The data arrives on a background thread and that is also where the event handler is called. You are not allowed to modify UI elements from a thread other than the one they were created on (the UI thread). If you search stackoverflow for this then you will find heaps of answers of how to do this. In Winforms you need to use InvokeRequired and in WPF you need to call the UI dispatcher.

One of the answers in this question contains a whole bunch of links to similar questions: Cross-thread operation not valid

Community
  • 1
  • 1
ChrisWue
  • 18,612
  • 4
  • 58
  • 83
0

As Chris says, Marshal the event onto the UI thread, see here. Furthermore, if any resource is likely to be accessed my multiple threads simultaneously then place that resource under mutex, see here.

Community
  • 1
  • 1
ldgorman
  • 1,553
  • 1
  • 14
  • 39