C
C#2mo 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
Nasdack2mo ago
What is the exception saying?
MKM
MKMOP2mo 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
Nasdack2mo ago
TRACKBAR_Throttle is a UI control that is created from within the main thread What UI framework are you using?
MKM
MKMOP2mo ago
winforms
Nasdack
Nasdack2mo 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
MKMOP2mo ago
okay so would i have to modify every single one of my methods and invoke them?
Nasdack
Nasdack2mo 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
MKMOP2mo ago
would i do this for all events?
Nasdack
Nasdack2mo ago
Any event that creates its own thread, yes
MKM
MKMOP2mo 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
Nasdack2mo 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
MKMOP2mo 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
Nasdack2mo ago
Not all of them, just the ones that are invoked by the calculation thread. What's unclean about it?
MKM
MKMOP2mo 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
Nasdack2mo ago
That is weird Not sure why But that fixes your problem anyway
MKM
MKMOP2mo ago
okay thank you very much ill try that now
Nasdack
Nasdack2mo ago
Np
MKM
MKMOP2mo ago
@Nasdack
No description
Nasdack
Nasdack2mo ago
there's a multitude of overloads your syntax is most likely incorrect
No description
Nasdack
Nasdack2mo ago
Send the rest of the code
Want results from more Discord servers?
Add your server