static void Main(string[] args) { Console.WriteLine("CurrentAppDomain start!"); //建立新的应用程序域对象 AppDomain newAppDomain = AppDomain.CreateDomain("newAppDomain"); //绑定CrossAppDomainDelegate的委托方法 CrossAppDomainDelegate crossAppDomainDelegate=new CrossAppDomainDelegate(MyCallBack); //绑定DomainUnload的事件处理方法 newAppDomain.DomainUnload += (obj, e) => { Console.WriteLine("NewAppDomain unload!"); }; //调用委托 newAppDomain.DoCallBack(crossAppDomainDelegate); AppDomain.Unload(newAppDomain) ; Console.ReadKey(); } static public void MyCallBack() { string name = AppDomain.CurrentDomain.FriendlyName; for(int n=0;n<4;n++) Console.WriteLine(string.Format( " Do work in {0}........" , name)); }
static void Main(string[] args) { var message = string.Format(" CurrentThreadID is:{0}\tAppDomainID is:{1}", Thread.CurrentThread.ManagedThreadId, AppDomain.CurrentDomain.Id); Console.WriteLine(message); Console.Read(); }
然后再新建一个ConsoleApplication项目,在此项目中新一个AppDomain对象,在新的AppDomain中通过ExecuteAssembly方法执行Example.exe程序。
static void Main(string[] args) { //当前应用程序域信息 Console.WriteLine("CurrentAppDomain start!"); ShowMessage(); //建立新的应用程序域对象 AppDomain newAppDomain = AppDomain.CreateDomain("newAppDomain"); //在新的应用程序域中执行Example.exe newAppDomain.ExecuteAssembly("Example.exe"); AppDomain.Unload(newAppDomain); Console.ReadKey(); } public static void ShowMessage() { var message = string.Format(" CurrentThreadID is:{0}\tAppDomainID is:{1}", Thread.CurrentThread.ManagedThreadId, AppDomain.CurrentDomain.Id); Console.WriteLine(message); }
class Program { [Synchronization] public class ContextBound : ContextBoundObject { public void Test() { ShowMessage(); } } static void Main(string[] args) { //当前应用程序域信息 Console.WriteLine("CurrentAppDomain start!"); ShowMessage(); //在上下文绑定对象中运行线程 ContextBound contextBound = new ContextBound(); contextBound.Test(); Console.ReadKey(); } public static void ShowMessage() { var message = string.Format(" CurrentThreadID is:{0}\tContextID is:{1}", Thread.CurrentThread.ManagedThreadId, Thread.CurrentContext.ContextID); Console.WriteLine(message); } }
本文出自勇哥的网站《少有人走的路》wwww.skcircle.com,转载请注明出处!讨论可扫码加群:


