在应用程序之间共享类属性(字段)
我有一个 8 位数字输出板,用于设备控制。每个外部设备需要一位并由不同的应用程序控制。 我编写了一个类库和类 DigitalOutputPort (VB 2010),它封装了管理 8 位端口的驱动程序。每个设备应用程序都使用此类,创建自己的实例。
为了设置数字输出端口的某个位,我必须向该端口写入一个字节:该字节是所有 8 位一起的位掩码:要将位数 0 - 1 - 2 设置为高电平,我必须写入7 在端口上,要将所有 8 位设置为高,我必须写入 256,依此类推...
当只有一个应用程序使用该类时,一切正常。但是,如果两个应用程序想要在该端口上设置自己的位,我就会遇到问题,因为我不知道其他应用程序设置的所有位的当前值(驱动程序没有这样的功能),当然,我无法更改一位不改变所有其他(如果我不知道当前的位掩码)
通常这看起来像是两个应用程序之间共享数据的典型情况,我的第一个想法是将端口的当前值写入光盘上的文件中,其中所有应用程序都可以访问和读取它。但对于这个简单的问题来说似乎太过沉重了。此外,它还可能造成性能和可靠性问题。
然后我考虑在类中使用共享字段(属性)。共享字段在类的所有实例之间保留其值:但在不同应用程序的实例之间也是如此吗?我无法找到有关最后一点的更多信息,我必须进行相同的测试。
第三种方法是我只创建 DigitalOutputPort 类的一个实例,一个用于所有应用程序。 第一个需要它的应用程序创建该对象,所有其他应用程序将使用已经创建的对象。 我比其他方式更喜欢这种方式,但我不知道是否可以以及如何完成。
您认为哪种方法应该是正确的?
谢谢您的回复。
I have an 8 bit digital output board used for device controlling. Each external device needs one bit and is controlled by a different application.
I have written a class library and the class DigitalOutputPort (VB 2010) that envelopes the driver that manages the 8 bit port. Each device application uses this class, creating its own instance.
In order to set a bit of the digital output port, I have to write a byte to that port: this byte is the bit mask for all 8 bits together: to set HIGH the bit number 0 - 1 - 2, I have to write 7 on the port, to set HIGH all 8 bits, I have to write 256, and so on...
All works fine when only ONE application uses the class. But if two applications want to set its own bit on that port, I get problems because I do not know the current value of all bits set by other applications (the driver has not such function) and, of course, I cannot change one bit without changing all the other (if I do not know the current bit mask)
Normally this looks like a typical case of sharing data between two application and my first idea was to write the current value of the port in a file on the disc, where all application can access and read it. But it seems to be too much heavy for this simple problem. Moreover it could also creates performance ans reliability problem.
Then I though about using a shared field (property) in the class. A shared field preserves its value between all instances of a class: but is it also true between instances from different applications? I cannot find more info about this last point, I have to make same test.
A third way would be that I create just only ONE instance of the class DigitalOutputPort, one for all applications.
The first application that needs it, create the object, all other applications will used the already created object.
I like more than other this way, but I do not know if and how it can be done.
Which should be the right approach in your opinion?
Thank you for replying.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
两个不同的应用程序始终具有不同且独立的内存。所以即使是共享字段也不会相同。共享字段仅在特定应用程序及其内存的上下文中共享,而不是在系统上全局共享。
因此,您需要在两个应用程序之间共享数据。有多种选择,但最简单和最简单的是您提到的一种 - 将其存储在磁盘上的文件中。这并不是矫枉过正,因为它是一个非常简单的实现。请记住不要锁定文件,因为多个进程需要访问它。
您提出的另一种可能性是使用 DigitalOutputPort 的共享实例。这意味着让第一个应用程序创建实例,并通过 WCF/Remoting/其他一些跨进程通信方法公开它,以便其他应用程序可以访问它。这当然是可能的(尽管一旦所有这些应用程序关闭,DigitalOutputPort 的状态就会丢失),但它要复杂得多,特别是如果您还没有使用这些通信框架的话。
我会坚持使用磁盘上的文件或注册表项来存储应用程序之间共享的持久数据。
Two different applications will always have distinct and separate memory. So even a Shared field will not be the same. A Shared field is shared only in the context of a specific application and its memory, not globally on the system.
So you need to share data between two applications. There are several options, though the simplest and easiest is the one you mentioned - store it in a file on disk. It's not overkill, since it's a very simple implementation. Just remember not to keep a lock on the file, since several processes will need to access it.
Another possibility you've raised is with a shared instance of DigitalOutputPort. This means having the first application create the instance, and expose it via WCF/Remoting/some other cross-process communication method so that other apps will access it. It's certainly possible (though the state of the DigitalOutputPort will be lost once all of these apps are closed), but it's a lot more complicated, especially if you don't already work with these communication frameworks.
I'd stick to a file on disk, or perhaps a registry key, to store shared, persistent data between applications.