how to execute code in a specific AppDomain

using System;
using System.Threading;

namespace LoadNewAppDomain
{
   class Program
   {
       static void Main(string[] args)
       {
           AppDomain domainA = AppDomain.CreateDomain("MyDomainA");
           AppDomain domainB = AppDomain.CreateDomain("MyDomainB");
           domainA.SetData("DomainKey", "Domain A value");
           domainB.SetData("DomainKey", "Domain B value");
           OutputCall();
           domainA.DoCallBack(OutputCall); //CrossAppDomainDelegate call
           domainB.DoCallBack(OutputCall); //CrossAppDomainDelegate call
           Console.ReadLine();
       }

       public static void OutputCall()
       {
           AppDomain domain = AppDomain.CurrentDomain;
           Console.WriteLine("the value {0} was found in {1}, running on thread Id {2}",
               domain.GetData("DomainKey"),domain.FriendlyName,
               Thread.CurrentThread.ManagedThreadId.ToString());
       }
   }
}

No comments:

Post a Comment