I have three classes - my main Form1
partial class, a class handling a state machine called SystemState
, and a class dealing with sending/receiving serial data called SerialCommunications
. I have separated these this way to keep my code modular, but so both Form1
and SystemState
can send serial data through the one instance of SerialCommunications
.
I can pass information from my classes to SerialCommunications
by having some public methods. My issue is that I would like my instance of SerialCommunications
to go back and update the UI on Form1
's thread, but I am not entirely sure how to approach this solution. If there is no better approach, how do I allow my class instance to receive an asynchronous serial message (via SerialDataReceivedEventArgs
) in one class, and use it to update the UI on another class and thread?
When I had my serial handling as part of Form1
's class in a previous iteration of the code, I was able to receive the asynchronous Serial message and update the UI with delegate
and Form1.Invoke
-related methods, though will admit I didn't quite understand how they worked. I believe similar concepts will work here, but now that this code has moved into a new class, I am unsure how to achieve what I would like. At the very least I know I can't use [class].Invoke anymore, because Form1
would be considered a control, but not the SerialCommunications
class.
I apologise for most of this coming from a lack of understanding some fairly simple concepts, but help is appreciated. The most promising leads I've found have been the following two links, but I haven't quite been able to work out how the apply the solutions to my system.
How to update UI from another thread running in another class