Re: Calling host AppDomain from new AppDomain

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



okaminer wrote:
I've tried what you've suggested
I've passed a delegate to the function but when i call that delegate
the function runs under the second Domain (new domain) and not the
host domain
This is the code

//Call back delegate
public delegate void CallBackDelegate(string str);
//Proxy class
public class ProxyClass:MarshalByRefObject
{
public void DoSomething(string str, CallBackDelegate callback)
{

System.Diagnostics.Debug.WriteLine(str + " from domain " +
AppDomain.CurrentDomain.Id);
//This function is called in the new AppDomain
callback("My Value");

You don't show the code that passes "callback", but whatever object the delegate is referring to must be a MarshalByRefObject created in the first AppDomain for the execution context to change. If it's serializable, the object will just be copied over to the new domain. Here's a complete sample:

class NewDomainObject : MarshalByRefObject {
public void DoCallback(Action callback) {
Console.WriteLine("NewDomainObject executing in domain " + AppDomain.CurrentDomain.FriendlyName);
callback();
}
}

class OldDomainObject : MarshalByRefObject {
public void PrintDomain() {
Console.WriteLine("OldDomainObject executing in domain " + AppDomain.CurrentDomain.FriendlyName);
}
}

class Program {
static void Main(string[] args) {
var newDomain = AppDomain.CreateDomain("New domain");
var oldDomainObject = new OldDomainObject();
var newDomainObject = (NewDomainObject) newDomain.CreateInstanceAndUnwrap(Assembly.GetExecutingAssembly().FullName, typeof(NewDomainObject).FullName);
newDomainObject.DoCallback(oldDomainObject.PrintDomain);
}
}

If you just need an isolated piece of code to execute in another AppDomain, you can also consider using AppDomain.DoCallBack(), but this approach is simpler.

--
J.
.



Relevant Pages

  • Re: Delegates...?
    ... I skipped trying to implement a list of listeners because I wanted to emphasize the "delegate" concept. ... Note how I use the interface called "Delegate" to make a method than can be called by the Presenter. ... public void doIt() { ... private Delegate model; ...
    (comp.lang.java.programmer)
  • Re: Global cache in Win Apps
    ... > I have to make a global cache in a windows application to store few ... will run the delegate. ... public void SetData ...
    (microsoft.public.dotnet.framework)
  • Re: Generic Delegate Meltdown From Constraint
    ... I think that using a delegate is just making the code more difficult to implement, and it will likely make the code harder to understand later. ... public MyOwned(UnsubscribeDelegate unsubscribe) ... public void MakeOwned() ... // that an actual type is supplied for the generic type ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: When is an event null?
    ... public delegate void MyEventHandler; ... MyEvent(); // NullReferenceException? ... The event will be null until an event handler is actually added to it. ... public void add_MyEvent ...
    (microsoft.public.dotnet.languages.csharp)
  • Protecting against null events
    ... constrain on a generic class based on the delegate type (compiler error ... It would also be nice if clients could add themselves to the handler ... throw new ArgumentException( ... public void AddHandler ...
    (microsoft.public.dotnet.languages.csharp)