是引用该库的所有进程共享的库(DLL)中的静态变量? [英] Is a static variable in a library (DLL) shared by all processes referencing that library?

查看:147
本文介绍了是引用该库的所有进程共享的库(DLL)中的静态变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道Web应用程序中使用的静态变量是通过Web应用程序为所有用户共享的。如果我有一个使用一些静态私有变量的库(DLL),那么使用该库的所有应用程序是否共享该变量的值?



例如,假设我有以下代码在我的DLL中:

  private static bool isConnected = false; 

public static void Connect()
{
// TODO:Connect。
isConnected = true;
}

public static void Disconnect()
{
// TODO:Disconnect。
isConnected = false;
}

然后在应用程序A中,我调用 myDLL.Connect () isConnected 的值设置为 True 。然后我有一些应用程序B做同样的事情。如果应用程序A稍后调用 myDLL.Disconnect(),则应用程序B将 isConnected 作为 False因为这两个应用程序与静态变量共享相同的DLL文件?在这种情况下,DLL文件在同一个文件路径中是同一个文件。



(以前我曾问过一个有关Web应用程序的类似问题其中。这是无关的。)

解决方案

不,他们不会。它们被分别加载到 AppDomains 中,看不到对方的实例。



即使它们引用相同的物理文件,每个应用程序都获得自己的私人实例


I know that a static variable used in a web application is shared for all users across the web application. If I have a library (DLL) that uses some static private variable, do all applications using that library share the value of that variable?

For example, say I have the following code in my DLL:

private static bool isConnected = false;

public static void Connect()
{
    // TODO: Connect.
    isConnected = true;
}

public static void Disconnect()
{
    // TODO: Disconnect.
    isConnected = false;
}

Then in Application A, I call myDLL.Connect() which sets the value of isConnected to True. Then I have some Application B that does the same thing. If Application A later calls myDLL.Disconnect(), does Application B see isConnected as False because the two applications share the same DLL file with a static variable? The DLL file would, in this case, be literally the same file in the same file path.

(I previously asked a somewhat similar question about web applications here. It is not related.)

解决方案

No they won't. They are loaded in separate AppDomains and cannot see each other's instances.

Even if they refer to same physical file, each application gets its own private instance of the assembly.

这篇关于是引用该库的所有进程共享的库(DLL)中的静态变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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