0

Situation:

I need to create some kind of controlling part of the communication over Modbus TCP/IP in my ASP.NET Core project.

Communication is working correctly with Modbus, but I need to read some Modbus addresses pernamently and I need to set them interval. I have such structure for this in db.

I have now singleton called CommunicationRepository, which is such controller for this protocol and have methods to read from the module, write to module, have structure for data from modules, etc. and create CommunicationRunners.

CommunicationRepository Diagram

CommunicationRunner is something that can wait to elapse the interval and than fire delegate from CommunicationRepository - which then read specific Modbus address.

CommunicationRunner Diagram

Problem: Now it is done by async, which is slowing down everything else - navigating throught the page, responses from web api, etc. Offcourse this problem is recognizable at small intervals (e.g. 10ms).

How can I make this more stable, non-bugging? Do I have to use threading? If, then how exactly?

I would appreciate some code samples.

Thank you and have a look at images.

David Pavelka
  • 373
  • 1
  • 5
  • 11
  • 1
    "Now it is done by async, which is slowing down everything else" << Does that mean it is executed during the user request?? At first it did sound like its a scheduler, but the above indicates it's not. If it's running purely in background and not in any way associated with a user request, then just put this part of your application into a console application and run it as separate process in the background (also know als background worker processes) – Tseng May 09 '17 at 10:34
  • It is not executed during the user request. It is supposted to be running in the background. Ok, that sounds great. I will make this as new project in the solution. Great suggestion - Thank you. And I can use .net core, so it remain cross-platform = perfect. – David Pavelka May 09 '17 at 10:55
  • And what am I supposed to use here? Could I use threads for each runner? What do you suggest? – David Pavelka May 09 '17 at 10:56
  • Highly depends on the specific requirements. You can run the main scheduler on the main thread and spin up new threads for each task (triggert after the interval expires). If you are using tracking (of the DbContext entities) you may need some factories to create a new instance of DbContext (or a new scope for the whole task using `IServiceScopeFactory`, this way all resources are freed up after the triggered action is finished). – Tseng May 09 '17 at 11:04
  • Or create commands (poco messages with information) and a `ICommandHandler` implementation which handles that command, use a message bus to distribute it among many servers... way to broad to explain it all in detail, since it depends on case by case – Tseng May 09 '17 at 11:04
  • Nice to hear about these options. I can have it on only one server and I am not using database in this communication (only for the first time to get what to read by modbus and get intervals, etc.). So I think that threads for each task is right way to do this. Thank you much. – David Pavelka May 09 '17 at 12:21

0 Answers0