.NET: Problem with raising and handling events using AppDomains

前端 未结 2 791
被撕碎了的回忆
被撕碎了的回忆 2020-12-09 06:39

Here is the basic gist of my problem:

  1. My main Window class instantiates Class A.
  2. Class A instantiates Class B in a secondary AppDomain
2条回答
  •  甜味超标
    2020-12-09 07:01

    In my first attempt at solving this issue, I removed Class B's inheritance of MarshalByRefObject and flagged it as serializable instead. The result was the the object was marshaled by value and I just got a copy of Class C that executes in the host AppDomain. This is not what I wanted.

    The real solution, I found, was that Class B (DangerousProgram in the example) should also inherit from MarshalByRefObject so that the call back also uses a proxy to transition the thread back to the default AppDomain.

    By the way, here's a great article I found by Eric Lippert that explains marshal by ref vs. marshal by value in a very clever way.

提交回复
热议问题