C
C#7h ago
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;


}
}
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
}
}
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
}
}
20 Replies
Nasdack
Nasdack7h ago
What is the exception saying?
MKM
MKM6h ago
{Value = {"Cross-thread operation not valid: Control 'TRACKBAR_Throttle' accessed from a thread other than the thread it was created on."} Min = 0 Max = 100} @Nasdack
Nasdack
Nasdack6h ago
TRACKBAR_Throttle is a UI control that is created from within the main thread What UI framework are you using?
MKM
MKM6h ago
winforms
Nasdack
Nasdack6h ago
Controls in WinForms provide an Invoke method so you that there you can execute actions on the thread from which they were created
MKM
MKM6h ago
okay so would i have to modify every single one of my methods and invoke them?
Nasdack
Nasdack6h ago
No I'm on my phone What you can do Is wrap the whole event handler method With TRACKBAR_Throttle.Invoke(() => { // existing method code});
MKM
MKM6h ago
would i do this for all events?
Nasdack
Nasdack6h ago
Any event that creates its own thread, yes
MKM
MKM6h ago
sorry just to make sure so i have this calculation thread and everything else on the ui thread, and so all events that need to be on ui thread need to be invoked because they canbe called whilst the calculation thread is running
Nasdack
Nasdack6h ago
If your calculation is running in its own thread, and it tries to invoke an event, that event will run on the thread that of the calculation logic, not that of the UI.
MKM
MKM6h ago
ah okay so i must invoke all my ui events becuase the calculation thread is always running is there a cleaner way to do this or is that it
Nasdack
Nasdack6h ago
Not all of them, just the ones that are invoked by the calculation thread. What's unclean about it?
MKM
MKM6h ago
the calculation thread doesnt invoke any though so im confused they can be invoked whilst the calculation thread is running but not by it
Nasdack
Nasdack6h ago
That is weird Not sure why But that fixes your problem anyway
MKM
MKM6h ago
okay thank you very much ill try that now
Nasdack
Nasdack6h ago
Np
MKM
MKM6h ago
@Nasdack
No description
Nasdack
Nasdack5h ago
there's a multitude of overloads your syntax is most likely incorrect
No description
Nasdack
Nasdack5h ago
Send the rest of the code
Want results from more Discord servers?
Add your server