C#C
C#12mo ago
ulrik brun

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();
        }



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
Was this page helpful?