Re: Calling host AppDomain from new AppDomain
- From: Jeroen Mostert <jmostert@xxxxxxxxx>
- Date: Sun, 30 Nov 2008 13:36:42 +0100
okaminer wrote:
I've tried what you've suggestedYou 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:
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");
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.
.
- References:
- Calling host AppDomain from new AppDomain
- From: okaminer
- Re: Calling host AppDomain from new AppDomain
- From: Jeroen Mostert
- Re: Calling host AppDomain from new AppDomain
- From: okaminer
- Calling host AppDomain from new AppDomain
- Prev by Date: Re: Calling host AppDomain from new AppDomain
- Previous by thread: Re: Calling host AppDomain from new AppDomain
- Index(es):
Relevant Pages
|