tags:

views:

356

answers:

2

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.

+4  A: 

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.

Mehrdad Afshari
Any pointers to good resources on how to do remoting?
Matthew Scharley
Nothing better than MSDN: http://msdn.microsoft.com/en-us/library/kwdt6w2k(VS.71).aspx This forum post contains a relevant code sample: http://social.msdn.microsoft.com/Forums/en-US/netfxremoting/thread/e2dc5846-ded5-4c01-acd1-2ac5f3285ec3
Mehrdad Afshari
+1  A: 

An object in .Net can only exist in one AppDomain. It is not possible for it to exist in 2 AppDomains at the same time.

However you can use .Net Remoting to push a proxy of a .Net object into several AppDomains at once time. This will give your object the appearance of being in multiple domains. I believe this is what you are looking for.

There are many tutorials available online. Google for ".Net Remoting Tutorial" and that will put you on teh right track.

JaredPar