C#C
C#15mo ago
33 replies
MKM

Trying to get started with threads but UI thread giving cross thread exceptions?

I have a thread that runs infinitely and what i believe is happening is that the new thread means events run on it leading to cross threading exceptions but i dont understand why that thread cant run in the background and events stay running on the main ui thread?

This is the test i am using for my new thread
private void CalculateEngineOutput() //Calculate some of the main outputs
  {
      while (true)
      {
         

          RevoloutionSpeed = 100 * Throttle;


      }
  }


and my throttle event handler is giving me threading exceptions but its on the main ui thread right?

 private void TRACKBAR_Throttle_Scroll(object sender, EventArgs e) //The trackbar throttle value is changed
 {
     //Gets the trackbar throttle value and removes the idle value from it to find how much the throttle is actually open

     if (TRACKBAR_Throttle.Value/100 <= (double)MainForm.ACCESSOR_GrabEngineObject.ACCESSOR_ATTRB_IdleThrottlePosition/100) //If the value is less than the idle throttle position
     {
         Throttle = (double)MainForm.ACCESSOR_GrabEngineObject.ACCESSOR_ATTRB_IdleThrottlePosition/100; //Set it to the idle throttle position
     }
     else
     {
         Throttle = TRACKBAR_Throttle.Value / 100; //Otherwise set it to the actual scroll bar value
     }
 }
Was this page helpful?