I'd like to know, if I have a variable,for example, a string, how to pass its value to my new app domain:
static string _str;
static void Main(string[] args) {
_str = "abc";
AppDomain domain = AppDomain.CreateDomain("Domain666");
domain.DoCallBack(MyNewAppDomainMethod);
AppDomain.Unload(domain);
Console.WriteLine("Finished");
Console.ReadKey();
}
static void MyNewAppDomainMethod() {
Console.WriteLine(_str); //want this to print "abc"
}
Thanks
I know that this is an old thread but perhaps this will help other people who are researching the subject.
In this article, the writer suggests using application domain
SetData
andGetData
methods for basic exchange of data objects that support marshal-by-value or marshal-by-reference object.Use one of the variations of AppDomain.CreateDomain that takes an AppDomainSetup argument. In the AppDomainSetup object, set the AppDomainInitializerArguments member to the string array you want passed to the new app domain.
See sample code at http://msdn.microsoft.com/en-us/library/system.appdomainsetup.appdomaininitializerarguments.aspx.
With the code in the question, the change might look like (not tested):
Addressing both your first and second requirements (passing through a value and retrieving another value back), here is a quite simple solution: