appdomain

指定托管应用程序的每个应用程序域应具有其特定全局变量或静态成员变量的副本。 有关更多信息,请参见应用程序域和 Visual C++

每个应用程序域都有其 a 的副本 per-appdomain 变量中。 appdomain 变量构造函数时,将执行程序集加载到应用程序域时,; 析构函数执行,当卸载应用程序域时。

如果希望在进程内的所有应用程序域在公共语言运行时共享全局变量,请使用 __declspec(process) 修饰符。 默认情况下__declspec(process) 确实位于 /clr 下,且默认情况下 __declspec(appdomain) 确实位于 /clr:pure下。 __declspec(appdomain) 强制在 /clr:safe下。

,当使用时,__declspec(appdomain) 才有效的某个 /clr 编译器选项。 全局变量、静态成员变量或仅将静态局部变量可以标记 __declspec(appdomain)。 ,因为它们一贯此行为,它是应用 __declspec(appdomain) 的错误于托管类型的静态成员。

使用 __declspec(appdomain) 类似于使用 线程本地存储区 (TLS)。 线程具有自己的存储,与应用程序域。 使用 __declspec(appdomain) 确保全局变量都有自己的存储在创建的每个应用程序域的此应用程序。

具有限制对混合使用每个 appdomain 变量处理和;请参见 处理 有关更多信息。

例如,在程序启动,所有 per-process 变量初始化,则所有 per-appdomain 变量初始化为。 因此,当 per-process 变量初始化时,它不能依赖于任何每个应用程序域变量的值。 不如习惯组合使用 () 分配 per-appdomain,并将依照进程。

有关如何调用特定应用程序域的函数的信息,请参见 call_in_appdomain 函数

示例

// declspec_appdomain.cpp
// compile with: /clr
#include <stdio.h>
using namespace System;

class CGlobal {
public:
   CGlobal(bool bProcess) {
      Counter = 10;
      m_bProcess = bProcess;
      Console::WriteLine("__declspec({0}) CGlobal::CGlobal constructor", m_bProcess ? (String^)"process" : (String^)"appdomain");
   }

   ~CGlobal() {
      Console::WriteLine("__declspec({0}) CGlobal::~CGlobal destructor", m_bProcess ? (String^)"process" : (String^)"appdomain");
   }

   int Counter;

private:
   bool m_bProcess;
};

__declspec(process) CGlobal process_global = CGlobal(true);
__declspec(appdomain) CGlobal appdomain_global = CGlobal(false);

value class Functions {
public:
   static void change() {
      ++appdomain_global.Counter;
   }

   static void display() {
      Console::WriteLine("process_global value in appdomain '{0}': {1}", 
         AppDomain::CurrentDomain->FriendlyName,
         process_global.Counter);

      Console::WriteLine("appdomain_global value in appdomain '{0}': {1}", 
         AppDomain::CurrentDomain->FriendlyName,
         appdomain_global.Counter);
   }
};

int main() {
   AppDomain^ defaultDomain = AppDomain::CurrentDomain;
   AppDomain^ domain = AppDomain::CreateDomain("Domain 1");
   AppDomain^ domain2 = AppDomain::CreateDomain("Domain 2");
   CrossAppDomainDelegate^ changeDelegate = gcnew CrossAppDomainDelegate(&Functions::change);
   CrossAppDomainDelegate^ displayDelegate = gcnew CrossAppDomainDelegate(&Functions::display);

   // Print the initial values of appdomain_global in all appdomains.
   Console::WriteLine("Initial value");
   defaultDomain->DoCallBack(displayDelegate);
   domain->DoCallBack(displayDelegate);
   domain2->DoCallBack(displayDelegate);

   // Changing the value of appdomain_global in the domain and domain2
   // appdomain_global value in "default" appdomain remain unchanged
   process_global.Counter = 20;
   domain->DoCallBack(changeDelegate);
   domain2->DoCallBack(changeDelegate);
   domain2->DoCallBack(changeDelegate);

   // Print values again
   Console::WriteLine("Changed value");
   defaultDomain->DoCallBack(displayDelegate);
   domain->DoCallBack(displayDelegate);
   domain2->DoCallBack(displayDelegate);

   AppDomain::Unload(domain);
   AppDomain::Unload(domain2);
}
  

请参见

参考

__declspec

C++关键字