fork(1) download
  1. using System;
  2. using System.Reflection;
  3. using System.Threading;
  4.  
  5. namespace TestTwoDomainsAndSingeThread
  6. {
  7. public class Worker : MarshalByRefObject
  8. {
  9. public void PrintDomain()
  10. {
  11. Console.WriteLine("Object is executing in AppDomain \"{0}\"",
  12. AppDomain.CurrentDomain.FriendlyName);
  13. Console.WriteLine("AppDomain for current thread: " + Thread.GetDomain().FriendlyName);
  14. }
  15. }
  16.  
  17. public class Test
  18. {
  19. static void Main(string[] args)
  20. {
  21. // Create an ordinary instance in the current AppDomain
  22. Worker localWorker = new Worker();
  23. localWorker.PrintDomain();
  24.  
  25. // Create a new application domain, create an instance
  26. // of Worker in the application domain, and execute code
  27. // there.
  28. AppDomain ad = AppDomain.CreateDomain("New domain");
  29. Worker remoteWorker = (Worker)ad.CreateInstanceAndUnwrap(
  30. typeof(Worker).Assembly.FullName,
  31. typeof(Worker).FullName);
  32. remoteWorker.PrintDomain();
  33. Console.ReadLine();
  34. }
  35. }
  36. }
Success #stdin #stdout 0.22s 30328KB
stdin
Standard input is empty
stdout
Object is executing in AppDomain "prog.exe"
AppDomain for current thread: prog.exe
Object is executing in AppDomain "New domain"
AppDomain for current thread: New domain