Wednesday, 15 February 2017

c# - "Cross thread operation not valid" .. while reading data on Serial Port



In a windows form application, on main form load, i have set a serial port and started reading it. The purpose is, as and when I receive some data on the serial port, I want to open another form related to the data.



So i use the DataReceived Event Handler of the serial port.



  void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)

{

string str = this.serialPort1.ReadLine();


if (!string.IsNullOrEmpty(str))
{


Main.Instance.customerData = new CustomerData(str);

Main.Instance.customerData.MdiParent = Main.Instance; //Exeption received at this point
Main.Instance.customerData.Disposed += new EventHandler(customerData_Disposed);

Main.Instance.customerData.Show();


}

}



But when I try to open a form within the event handler it gives me an InvalidOperationExeption saying:
"Cross-thread operation not valid: Control 'Main' accessed from a thread other than the thread it was created on."



I tried removing the code line :
Main.Instance.customerData.MdiParent = Main.Instance;
then it works fine. But its necessary also to assign the mdiparent in order to open it as a child form.



Any suggestions to resolve this problem ?


Answer




Use the Invoke method on the Main form. You have to pass control over to the Main form to interact with it. The event handler is triggered on a background thread.



Here's some sample code that may work:



void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
string str = this.serialPort1.ReadLine();
if (!string.IsNullOrEmpty(str))
{
ShowCustomerData(str);

}

}

private delegate void ShowCustomerDataDelegate(string s);

private void ShowCustomerData(string s)
{
if (Main.Instance.InvokeRequired)
{

Main.Instance.Invoke(new ShowCustomerDataDelegate(ShowCustomerData), s);
}
else
{

Main.Instance.customerData = new CustomerData(str);
Main.Instance.customerData.MdiParent = Main.Instance; //Exeption received at this point
Main.Instance.customerData.Disposed += new EventHandler(customerData_Disposed);

Main.Instance.customerData.Show();

}
}

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...