Is a static variable in a library (DLL) shared by all processes referencing that library?

旧时模样 提交于 2019-11-29 05:57:06

问题


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.)


回答1:


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.



来源:https://stackoverflow.com/questions/6818052/is-a-static-variable-in-a-library-dll-shared-by-all-processes-referencing-that

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!