Search code examples
c#multithreadingmarshallingappdomain

C# AppDomains and Threads


I've come with a little question that keeps me awake all night. Maybe it's easy to understand, but I can't, yet.

Which is better? An AppDomain inside a Thread, or a Thread inside a new AppDomain?

So which is the difference between:

static void Main() {
    AppDomain ad = AppDomain.CreateDomain ("NewDomain");
    Thread t = new Thread (delegate() { ad.DoCallBack (SomeMethod); });
    t.Start();
}

static void SomeMethod() { }

And:

public void Start() {
    myAssembly = Assembly.LoadFrom(dllFileName);
    Type myType = AssEnsamblado.GetType(myAseembly.Type);
    MethodInfo myMethod = tipo.GetMethod(@"StartDLL");
    object obj = Activator.CreateInstance(myType);
    Thread thrBase = new Thread(new ThreadStart(delegate() { myMethod.Invoke(obj, null); }));
    thrBase.Start();
}

The snippet above is being executed in a class which inherits from MBRO, to create a remote instance and call the method that contains that code.


Solution

  • I would take a risk and say that once your code is executing in a new Application Domain, it makes little or no difference at all either way.

    The issue is the crossing to the other application domain, which may require serialization/marshalling of objects (if it supports it) that are passed/returned to the new application domain.