I have an industrial equipment which is controlled by a software. The software connects to it using sockets, and keeps the connection all the time alive, while from time to time sending commands to the equipment. Now, this equipment is represented by a class which encapsulates the sending and receiving of data.
Next, I need to make a UI, that will show the state of the equipment, independent from the controlling software (controlling is on a different thread same solution). Refresh to the UI must be done lets say every second, but it can happen that UI wants to read data, and the controlling software wants to write.
The problem is that the equipment might respond from 50 ms to 1 second, we don't know, and during the response no one must interfere!
How can I best synchronize calls to the equipment communication class?
There might be at least 2 solutions:
- Using simple
lock(obj) {}
statement in C#, for each method in the common. class - Using events and a message queue with a subscriber concept
While the first one might be simple, I assume for my case is the worst solution because it can block everything.
The second one seems more OK, but the problem is that there will be for sure some delay on receiving the results if many subscribers are, or dunno, I'm not really sure.
UPDATE: I implemented using simple lock. The reason is that some equipment do not have (some do, but most not), on their controller side, the possibility to process multiple request, and their buffer will overflow. So to make sure nothing bad happens, exclusive READ/WRITE lock is best solution I have found so far, and it works.