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,
Not sure if this will help, but in my sample project Blazor Image Gallery, I login a user in another thread like this:
Then my ProcessLogin method looks like this:
If you want to see a full working version, here is a sample project and video:
Code: https://github.com/DataJuggler/BlazorImageGallery
Video: Building Blazor Image Gallery https://youtu.be/3xKXJQ4qThQ
Maybe it will give you some ideas, not sure if this will help you or not.