I'm using an object that allows me to subscribe to events:
easyModbusTCPServer.holdingRegistersChanged += new EasyModbus.ModbusServer.HoldingRegistersChanged(RegistersChanged);
Whenever this event is triggered the function "RegistersChanged" is called and it starts or stops some "long running code" (stopping happens by changing a boolean "StopRunning" to "True")
Stopping the "long running code" unfortunately does not work. I think since the "long running code" is busy in the main thread, it blocks the eventhandler "registersChanged" from being reached....
Is it possible to have the function "RegistersChanged" called in a different thread, so the function "RegistersChanged" is still called and the "long running code" in the main thread can be stopped.
Instead of putting the eventhandler in a seperate thread, I could place the long running code in a seperate thread, but unfortunately this "long running code" is complex and requires communication with a lot of objects created in the main thread....
It is not important that the UI cannot be used while the code is running.
Thank you,
Short answer: No, unless you can modify that code that actually raises the event. Then you can raise it on another thread. The event handler gets invoked on the same thread where the event is raised.
You can switch to another thread the first thing do inside the RegistersChanged event handler though.
Then you will have to rewrite it.