VB.NET中各种对象(共享类,模块等)的广泛可见性

时间:2023-01-18 19:09:18

I have a VB.NET application where various objects are going to access some common code, and I have some counters and values shared between all the calls, so I'm currently using a "Shared Class" (I'm aware classes can't be shared, per se, but all the variables and methods are marked "Shared").

我有一个VB.NET应用程序,其中各种对象将访问一些公共代码,并且我在所有调用之间共享一些计数器和值,所以我当前正在使用“共享类”(我知道类可以'本身是共享的,但所有变量和方法都标记为“共享”)。

My concern is for the visibility of this object - if a user runs two instances of the application on the same desktop, will they use the same "instance" of the shared class? In other words, will the two copies of the application both increment the same counters and use the same variables?

我关心的是这个对象的可见性 - 如果用户在同一个桌面上运行两个应用程序实例,他们会使用共享类的相同“实例”吗?换句话说,应用程序的两个副本是否会增加相同的计数器并使用相同的变量?

In addition, what about two user spaces on the same machine - if each user runs a copy, will those two copies interfere?

另外,同一台机器上的两个用户空间怎么样 - 如果每个用户都运行一个副本,这两个副本会干扰吗?

Perhaps I'm bastardizing the "Shared" concept and there's a much better way to do this (I know some people do shared code through Modules instead of classes). Is there a better method of having objects (both variables and methods) shared across the entire application, but kept separate from other application instances?

也许我正在混淆“共享”概念,并且有更好的方法来做到这一点(我知道有些人通过模块而不是类共享代码)。是否有更好的方法让整个应用程序共享对象(包括变量和方法),但与其他应用程序实例分开?

UPDATE: In response to the question about my "preferred" way, the application I'm writing needs keep data private, even between different instances that the user is running. In that case, I don't want to share any data at all between multiple instances, which is why I'm concerned about the side-effects of using a Shared class or a Module. Thanks for the question.

更新:在回答关于我的“首选”方式的问题时,我正在编写的应用程序需要将数据保密,即使在用户运行的不同实例之间也是如此。在这种情况下,我不想在多个实例之间共享任何数据,这就是为什么我担心使用共享类或模块的副作用。谢谢你的提问。

2 个解决方案

#1


Each instance of your application, whether run by a single user or multiple, will see its own copy of Shared fields.

无论是由单个用户还是多个用户运行,您的应用程序的每个实例都将看到自己的共享字段副本。

This blog entry goes into more detail, describing cases where static/Shared fields are scoped to items like threads rather than the default context, an AppDomain.

此博客条目更详细,描述了静态/共享字段的范围限定为线程等项而非默认上下文(AppDomain)的情况。

#2


Shared values (or Static if you are a C#er) are only common to the App Domain.

共享值(如果您是C#er,则为Static)仅适用于App Domain。

It is even possible to load multiple App Domains into the same process and each will have its own set of Shared values.

甚至可以将多个应用程序域加载到同一个进程中,每个应用程序域都有自己的共享值集。

#1


Each instance of your application, whether run by a single user or multiple, will see its own copy of Shared fields.

无论是由单个用户还是多个用户运行,您的应用程序的每个实例都将看到自己的共享字段副本。

This blog entry goes into more detail, describing cases where static/Shared fields are scoped to items like threads rather than the default context, an AppDomain.

此博客条目更详细,描述了静态/共享字段的范围限定为线程等项而非默认上下文(AppDomain)的情况。

#2


Shared values (or Static if you are a C#er) are only common to the App Domain.

共享值(如果您是C#er,则为Static)仅适用于App Domain。

It is even possible to load multiple App Domains into the same process and each will have its own set of Shared values.

甚至可以将多个应用程序域加载到同一个进程中,每个应用程序域都有自己的共享值集。