A Complete multithreaded console program that is using several app domains

T

Tony Johansson

Hi!

Here I have a console program that is creating 5 app domain running an exe
in each one.
Now assume that I want to run 5 exe files called Foo.exe.
If I instead only created one app domain and started 5 threads running the
Foo.exe in each thread which solution od these two would be the best choice
?

I would say that using 5 app domain running an exe in each would be th ebest
solution.
Do you agree with me ?


using System;
using System.Threading;

public class MyApp
{
private int sleepTime = 1000;
private AppDomain appDom;
private Thread appThrd;
private AppDomainSetup appSetup;

public MyApp(string domain, string process) //create 5 AppDomain domain
in the C-tor
{
appSetup = new AppDomainSetup();
appSetup.ApplicationName = process;
appSetup.ApplicationBase = "file:///" +
System.Environment.CurrentDirectory;
appDom = AppDomain.CreateDomain(appSetup.ApplicationName + " - " +
domain, null, appSetup);
}

public void Run()
{
appThrd = new Thread(new ThreadStart(this.RunApp));
appThrd.Start();
}

private void RunApp()
{
string[] args = new string[] { sleepTime.ToString() };
appDom.ExecuteAssembly(appSetup.ApplicationName);
Console.WriteLine(appSetup.ApplicationName + " started.");
}
}
public class Program
{
static void Main(string[] args)
{
MyApp[] apps = new MyApp[5];
string appName = "Foo.exe";

for (int i = 0; i < 5; i++)
{
apps = new MyApp(("Application" + (i + 1)), appName);
apps.Run();
}
}
}

//Tony
 
P

Peter Duniho

Tony said:
Hi!

Here I have a console program that is creating 5 app domain running an exe
in each one.
Now assume that I want to run 5 exe files called Foo.exe.
If I instead only created one app domain and started 5 threads running the
Foo.exe in each thread which solution od these two would be the best choice
?

Best choice given what goals?

As has been explained to you, the point of using the AppDomain is mainly
so that you can isolate data and the executing code.

If you need to have that isolation for each executing assembly, then
yes…an AppDomain for each thread is needed. Otherwise, it's not.

As far as I know, you can only retrieve the return value from the
assembly's entry point when you use ExecuteAssembly(). Frankly, it
seems to me that executing an assembly using the Process class gives you
at least as good isolation as an AppDomain would, is more
straightforward to use, _and_ allows for redirection of the standard
streams for the process (input, output, and error).

So the whole question seems a bit contrived to me.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top