1

Let's say I have a windows service called "MyService" and an executable called "MyEXE"

Is it possible (from within "MyService") to start several instances of "MyEXE" running in seperate application domains in parallel?

I would apprecaiate if some one can also provide a small sample using .net.

H H
  • 263,252
  • 30
  • 330
  • 514
Raju
  • 114
  • 1
  • 12

1 Answers1

4

As long as it is a managed program then, yes, you can run it in its own AppDomain. You'll need a thread to run the code, AppDomain.ExecuteAssembly() is the handy one that automatically starts running the Main() method of that program. Here's an example that uses two console mode applications:

using System;
using System.Threading;
using System.IO;

namespace ConsoleApplication1 {
    class Program {
        static void Main(string[] args) {
            string exePath = @"c:\projects\consoleapplication2\bin\debug\consoleapplication2.exe";
            for (int ix = 0; ix < 10; ++ix) {
                var setup = new AppDomainSetup();
                setup.ApplicationBase = Path.GetDirectoryName(exePath);
                var ad = AppDomain.CreateDomain(string.Format("Domain #{0}", ix + 1), null, setup);
                var t = new Thread(() => {
                    ad.ExecuteAssembly(exePath);
                    AppDomain.Unload(ad);
                });
                t.Start();
            }
            Console.ReadLine();
        }
    }
}

And the one that's ran 10 times:

using System;

namespace ConsoleApplication2 {
    class Program {
        static void Main(string[] args) {
            Console.WriteLine("Hello from {0}", AppDomain.CurrentDomain.FriendlyName);
        }
    }
}

One thing I didn't count on and stuck under a table a bit, the AppDomainSetup.ApplicationBase property didn't work as I expected. I had to pass the full path of the EXE to ExecuteAssembly() instead of just passing "consoleapplication2.exe". That was odd.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Hans,This is exactly what I was trying to do. Thanks a lot for your help. – Raju Oct 28 '10 at 22:04
  • Hans, Is it possible to check if the appdomain which was created is active after calling execute assembly method. Please let me know. Thanks in Advance, Raju – Raju Mar 01 '11 at 22:14
  • "Active" doesn't mean anything for an AppDomain. It exists. – Hans Passant Mar 01 '11 at 22:24
  • Hans, What I meant is to check if the appdomain created to run a job is still running or not. How do I know that from the parent appdomain? Also How do I know the appdomains that I created from the parent domain, I was looking for a collection but could not find any. I appreciate your help. Thanks, Raju – Raju Mar 02 '11 at 05:00
  • AppDomains don't "run", a thread runs. You can unload it safely after you know that any thread you started to run in an AD has ended. CreateDomain returns a reference, store it somewhere. Enumerating them later is not directly supported. – Hans Passant Mar 02 '11 at 05:20