开发者

C# AppDomains and Threads

开发者 https://www.devze.com 2023-03-20 15:19 出处:网络
I\'ve come with a little question that keeps me awake all night. Maybe it\'s easy to understand, but I can\'t, yet.

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 wich is the diference 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开发者_JAVA技巧 witch inherits from MBRO, to create a remote instance and call the method that contains that code.


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.


Steer away from such code (either of the samples). It will make your life hell, unless you enjoy spending weeks in debugging weird exceptions here and there.

Face the root problem - shut down the remote job gracefully. There are usually many ways to do that, including different ways to cancel or terminate it, but calling a dirty hack Thread.Abort() is just evil.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号