是引用该库的所有进程共享的库(DLL)中的静态变量? [英] Is a static variable in a library (DLL) shared by all processes referencing that library?
问题描述
例如,假设我有以下代码在我的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屋!