Sharing a class property (field) between applications

I have an 8 bit digital output board used to control a device. Each external device requires one bit and is controlled by another application. I wrote a class library and a class DigitalOutputPort (VB 2010), which converts the driver that controls the 8-bit port. Each device application uses this class, creating its own instance.

To set the digital output port bit, I have to write a byte to this port: this byte is a bit mask for all 8 bits together: to set HIGH bit 0 - 1 - 2, I need to write 7 to the port, set HIGH all 8 bits, I need to write 256 and so on ...

Everything works fine when only one application uses the class. But if two applications want to set their own bit on this port, I have problems because I do not know the current value of all the bits set by other applications (the driver does not have such a function), and, of course, I cannot change one bit without changing all the others (if I do not know the current bitmask)

This usually looks like a typical case of data sharing between two applications, and my first idea was to write the current port value in a file on disk, where the entire application can access and read it. But for this simple problem, it is too hard. In addition, it can also create a problem of reliability and reliability.

Then I, however, talk about using a common class (property) in a class. A common field retains its value between all instances of the class: but is this also true between instances from different applications? I can not find more information about this last point, I have to do the same test.

The third way is to create only one instance of the DigitalOutputPort class, one for all applications. The first application that needs it will create an object, all other applications will use the already created object. I like it more than others, but I don’t know whether and how to do it.

What should be the right approach in your opinion?

Thanks for answering.

0
source share
1 answer

Two different applications will always have separate and separate memory. Thus, even the Shared field will not be the same. A common field is shared only in the context of a specific application and its memory, and not on a global scale in the system.

Therefore, you need to exchange data between two applications. There are several options, although the simplest and easiest β€” the one you mentioned β€” save it to a file on disk. This is not too complicated, as it is a very simple implementation. Just remember that do not store the lock in the file, as accessing it requires several processes.

Another opportunity you raised is the generic instance of DigitalOutputPort. This means that the first application created an instance and brought it out through the WCF / Remoting / some other inter-process communication method so that other applications can access it. This is of course possible (although the DigitalOutputPort state will be lost after closing all these applications), but it is much more complicated, especially if you are not working with these communication infrastructures yet.

I would use a file on disk or perhaps a registry key to store shared, persistent data between applications.

+1
source

Source: https://habr.com/ru/post/1396382/


All Articles