简体   繁体   中英

How to run method from assembly loaded to references, in new AppDomain

Recently i discover that running few instances of method compiled to .exe is faster than running the same method in fe few new Tasks. I don't know if that applies to every method, but it does to getting data from API.

I was searching internet to find answer how to manage that. I got answers to try run method in new appDomains. So i create .exe assembly with methods that i want to be run (it is Console application). I Load it by right click on References -> Add Reference. I could easily access that method by exeName.ClassName.Method(params). The thing is that I don't know how to run this methods in new appDomains. Every answer that i found in web was with loaded assembly by path.

I will also be very happy for answers other than creating AppDomain. I just want to pass data to this method and get results.

TL;DR: Method run in Parallel.For(0,4,i=> method()) works slower than run the same method in 4 instances of compiled .exe file.

You could use a multi process architecture using IPC protocol or host your methods inside different domains. In both situations i recommend .net remoting over wcf because you would write almost the same code for both aproaches and because for talking to a class found in another app domain hosted in the same process, .net remoting is the only way (sadly for many devs but not for me). BUT I am almost sure that generally this would NOT be more faster than just creating some threads and calling them asinchronous. Inter domains / process communication must rely on message serialization/ deserialization that adds huge overhead, specially if the method call itself is very light.

After some researching and asking i found solution:

var ad = AppDomain.CreateDomain("mydomain");
ad.DoCallBack(() =>
            {
                //stuff to do
            }

Probably there'll be some issues with passing data to new AppDomain. Easiest way for me is:

ad.SetData("key", value);

and to retrive in AppDomain:

var value = (valueType)AppDomain.CurrentDomain.GetData("key");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM