C
C#3d ago
USB

Crashes on events trying to read data across threads

Error: An exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll but was not handled in user code The cross-thread operation is invalid: The control simconnectstatus was obtained from a different thread than the thread on which it was created. Using .net framework 4.7.2 Code Where it crashes:
public partial class Form1 : Form
{
Connect connect = new Connect();
DefSimvars defSimvars = new DefSimvars();
public Form1()
{
InitializeComponent();
connect.OnData += Receivedata;
}

public void altitude_label_Click(object sender, EventArgs e)
{
}

public void simconnectstatus_Click(object sender, EventArgs e)
{

}

private void simconnect_logon_Click(object sender, EventArgs e)
{
this.simconnect_logon.Enabled = false;
this.simconnectstatus.Text = "Simconnect: Connecting...";
connect.Connectsim();
this.simconnect_logon.Enabled = true;

}

public void Receivedata(object sender, EventArgs e)
{
DefSimvars ab = (DefSimvars)e;
this.simconnectstatus.Text = ab.SimVars.message;
//this.latitude_label.Text = defSimvars.SimVars.latitude.ToString();
//this.longitude_label.Text = defSimvars.SimVars.longitude.ToString();
//this.altitude_label.Text = defSimvars.SimVars.altitude.ToString();
}
public partial class Form1 : Form
{
Connect connect = new Connect();
DefSimvars defSimvars = new DefSimvars();
public Form1()
{
InitializeComponent();
connect.OnData += Receivedata;
}

public void altitude_label_Click(object sender, EventArgs e)
{
}

public void simconnectstatus_Click(object sender, EventArgs e)
{

}

private void simconnect_logon_Click(object sender, EventArgs e)
{
this.simconnect_logon.Enabled = false;
this.simconnectstatus.Text = "Simconnect: Connecting...";
connect.Connectsim();
this.simconnect_logon.Enabled = true;

}

public void Receivedata(object sender, EventArgs e)
{
DefSimvars ab = (DefSimvars)e;
this.simconnectstatus.Text = ab.SimVars.message;
//this.latitude_label.Text = defSimvars.SimVars.latitude.ToString();
//this.longitude_label.Text = defSimvars.SimVars.longitude.ToString();
//this.altitude_label.Text = defSimvars.SimVars.altitude.ToString();
}
I don't know what todo, i'm trying to send a structure data from my connect.cs file back to form1.cs using evnets, here i am trying to call the ondata event and it works just fine but at bottom where i define event args E as defsimvars withc is a class that contains a structure i wanna use i get this error message
2 Replies
USB
USBOP3d ago
simconnectstatus is a text label in winform, the error means it's trying to reach it from a diffenrent thread. I don't know how to get around that
Sehra
Sehra3d ago
only the ui thread is allowed to interact with the controls, so you have to marshal the request to that thread. something like this
if (InvokeRequired) {
Invoke(() => { simconnectstatus.Text = "..."; });
}
if (InvokeRequired) {
Invoke(() => { simconnectstatus.Text = "..."; });
}

Did you find this page helpful?