fork(7) 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. Console.WriteLine("Current thread ID: " + Thread.CurrentThread.ManagedThreadId.ToString());
  15. }
  16. }
  17.  
  18. public class Test
  19. {
  20. static void Main(string[] args)
  21. {
  22. // Create an ordinary instance in the current AppDomain
  23. Worker localWorker = new Worker();
  24. localWorker.PrintDomain();
  25.  
  26. // Create a new application domain, create an instance
  27. // of Worker in the application domain, and execute code
  28. // there.
  29. AppDomain ad = AppDomain.CreateDomain("New domain");
  30. Worker remoteWorker = (Worker)ad.CreateInstanceAndUnwrap(
  31. typeof(Worker).Assembly.FullName,
  32. typeof(Worker).FullName);
  33. remoteWorker.PrintDomain();
  34. Console.ReadLine();
  35. }
  36. }
  37. }
Success #stdin #stdout 0.29s 33244KB
stdin
Standard input is empty
stdout
Object is executing in AppDomain "prog.exe"
AppDomain for current thread: prog.exe
Current thread ID: 1
Object is executing in AppDomain "New domain"
AppDomain for current thread: New domain
Current thread ID: 1