在应用程序之间共享类属性(字段) [英] Sharing a class property (field) between applications

查看:136
本文介绍了在应用程序之间共享类属性(字段)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个8位数字输出板用于设备控制。每个外部设备需要一个位,并由不同的应用程序控制。
我写了一个类库和DigitalOutputPort类(VB 2010),它包含管理8位端口的驱动程序。每个设备应用程序都使用这个类,创建自己的实例。

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.

为了设置数字输出端口的位,我必须写一个字节到该端口:this字节是所有8位的位掩码:设置为高位的位数0 - 1 - 2,我必须在端口上写7,设置高全8位,我必须写256,等等。 。

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.

第三种方法是,我只创建一个类DigitalOutputPort的一个实例,一个适用于所有应用。
第一个需要它的应用程序,创建对象,所有其他应用程序将使用已创建的对象。
我喜欢比其他更多的方式,但我不知道如何和如何做。

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?

感谢您回覆。

推荐答案

有独立和单独的内存。所以即使一个共享字段也不一样。共享字段仅在特定应用程序及其内存的上下文中共享,而不是在系统上全局共享。

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.

因此,您需要在两个应用程序之间共享数据。有几个选项,虽然最简单和最简单的是你提到的 - 将其存储在磁盘上的文件。这不是overkill,因为它是一个非常简单的实现。记住不要对文件保持锁定,因为有几个进程需要访问它。

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.

你提出的另一种可能性是使用DigitalOutputPort的共享实例。这意味着让第一个应用程序创建实例,并通过WCF / Remoting /一些其他跨进程通信方法公开它,以便其他应用程序访问它。这当然是可能的(虽然所有这些应用程序关闭后,DigitalOutputPort的状态将会丢失),但是它更复杂,特别是如果你还没有使用这些通信框架。

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.

这篇关于在应用程序之间共享类属性(字段)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆