Home   Cover Cover Cover Cover
 

Application domains

Question: Write a C# program AppLauncher that can launch and run other applications. Each of these applications should run in its own application domain (not in a separate process!).

Answer: see 3.7.1 Application Domains

AppLauncher.cs
using System;
using System.Threading;

class AppLauncher {
  class AppThread {
    AppDomain domain;
    string uri;

    public AppThread (string appURI) {
      uri = appURI;
      domain = AppDomain.CreateDomain(uri);
    }

    public void Execute () {
      try {
        domain.ExecuteAssembly(uri);
      } catch (Exception e) {
        Console.WriteLine("Exception while executing " + domain.FriendlyName);
        Console.WriteLine(e);
      }
    }
  }

  static void Main () {
    for (;;) {
      Console.WriteLine();
      Console.Write(">");
      string appURI = Console.ReadLine();

      try {
        if (appURI == "exit") break;
        else if (if (appURI == null || appURI.Length == 0)
          throw new Exception("No application specified");

        AppThread at = new AppThread(appURI);
        Thread t = new Thread(new ThreadStart(at.Execute));
        t.Start();
      } catch (Exception e) {
        Console.WriteLine(e);
      }
    }
  }
}