Some learning about AppDomain.DoCallBack

Let's first see the MSDN document about this method: http://msdn.microsoft.com/en-us/library/system.appdomain.docallback.aspx

The three examples are good, but helping yourself to understand better, you may need to twist them a little...

Summary (the following is based on my understanding, for any mistake please comment below :))

There are three scenarios for DoCallBack method applies:

1. use Static DoCallBack method,
In this, neither default or new domain owns the field and MyCallBack code, they share the same.

2. use DoCallBack method by value, --modified with [Serializable]
In this, default domain owns the first-hand data and the new domain just owns a second-hand copy.

3. use DoCallBack method by reference, -- let class inherit from MarshalByRefObject
In this, default domain owns the only real data but the new domain just keep a reference to the data (class fields and methods).

Hope this explains better, addition to the MSDN document.

So, we can also explain why everytime we try to make a new Form in a new Domain, it always seems to be failed... as Form class inherit from MarshalByRefObject, and in a new Domain it only keeps the reference, yes just a reference, all the Callbacks and data happen in the Default Domain actually.

posted @ 2009-03-16 17:00  Figo Fei  阅读(515)  评论(0编辑  收藏  举报