Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AppDomain and threading

Tags:

c#

appdomain

Basically, from what I've understood of the little I've managed to search up on the internet, threads can pass between AppDomains. Now, I've written the following code:

    const string ChildAppDomain = "BlahBlah";
    static void Main()
    {
        if (AppDomain.CurrentDomain.FriendlyName != ChildAppDomain)
        {
            bool done = false;
            while (!done)
            {
                AppDomain mainApp = AppDomain.CreateDomain(ChildAppDomain, null, AppDomain.CurrentDomain.SetupInformation);
                try
                {
                    mainApp.ExecuteAssembly(Path.GetFileName(Application.ExecutablePath));
                }
                catch (Exception ex)
                {
                    // [snip]
                }
                AppDomain.Unload(mainApp);
            }
        }
        else
        {
            // [snip] Rest of the program goes here
        }
    }

This works fine and everything is clicking into place... The main thread passes through into the new version of my program and starts running through the main application body. My question is, how would I go about getting it to go back out to the parent AppDomain? Is this possible? What I'm trying to achieve is sharing an instance of a class between the two domains.

like image 969
Matthew Scharley Avatar asked Jan 23 '23 11:01

Matthew Scharley


1 Answers

You cannot share instances of classes directly between AppDomains. To do so, you should derive the class from MarshalByRefObject and use remoting to access the instance from the other AppDomain.

like image 72
mmx Avatar answered Jan 26 '23 00:01

mmx