Raising events in multithreaded classes?
I am running a class(gamepad handler) that uses many child threads to check for key input and the like then it raises events to my form to sort out the needed reaction, Is there a way to make the event raises on the same thread as the class itself.
The simpliest answer is, “just raise the event on the current thread.” It is up to the form to handle the events then perform any updates on the form’s thread using
Control.Invoke.Here is the recommended way to update a
Labelcontrol called__messagefrom another thread.Add the following code to the form.
Then call form
.SetText(<messageToDisplay>)where needed.You can either use the
Control.InvokeorControl.BeginInvokemethods. SeeControl.InvokeRequiredProperty for more information.