title: .NET: Problem with raising and handling events using AppDomains
body: <p>Here is the basic gist of my problem:</p>
  
  <ol>
  <li>My main Window class instantiates Class A.</li>
  <li>Class A instantiates Class B in a <strong>secondary AppDomain</strong>.</li>
  <li>Class B raises an event and Class A handles the event successfully.</li>
  <li>Class A raises an event of its own.</li>
  </ol>
  
  <p><strong>Problem:</strong> In step 4, when Class A raises its <em>own</em> event from the event handler method that caught Class B's event, the event is raised; <strong>however,</strong> the subscribing handler in the Window class is never called.  </p>
  
  <p>There are no exceptions being thrown.  If I remove the secondary AppDomain, the event gets handled without a problem.</p>
  
  <p>Does anyone know why this doesn't work?  Is there another way to make this work without using a callback?</p>
  
  <p>I would think, if anything, the problem would occur in step 3 instead of step 4.</p>
  
  <p>Here's a real code sample to illustrate the problem:</p>
  
  <pre><code>Class Window1
  
      Private WithEvents _prog As DangerousProgram    
  
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click    
          _prog = New DangerousProgram()
          _prog.Name = "Bad Program"  
      End Sub
  
      Private Sub MyEventHandler(ByVal sender As Object, ByVal e As NameChangedEventArgs) Handles _prog.NameChanged
          TextBox1.Text = "Program's name is now: " &amp; e.Name
      End Sub
  
  End Class
  
  
  &lt;Serializable()&gt; _    
  Public Class DangerousProgram
  
      Private _appDomain As AppDomain
      Private WithEvents _dangerousProgram As Program
      Public Event NameChanged(ByVal sender As Object, ByVal e As NameChangedEventArgs)
  
  
      Public Sub New()
  
          // DangerousPrograms are created inside their own AppDomain for security.
  
          _appDomain = AppDomain.CreateDomain("AppDomain")    
          Dim assembly As String = System.Reflection.Assembly.GetEntryAssembly().FullName 
          _dangerousProgram = CType( _   
                      _appDomain.CreateInstanceAndUnwrap(assembly, _    
                          GetType(Program).FullName), Program)
  
      End Sub
  
  
      Public Property Name() As String
          Get
              Return _dangerousProgram.Name
          End Get
          Set(ByVal value As String)
              _dangerousProgram.Name = value
          End Set
      End Property
  
  
      Public Sub NameChangedHandler(ByVal sender As Object, ByVal e As NameChangedEventArgs) Handles _dangerousProgram.NameChanged    
          Debug.WriteLine(String.Format("Caught event in DangerousProgram. Program name is {0}.", e.Name))
          Debug.WriteLine("Re-raising event...")
  
          RaiseEvent NameChanged(Me, New NameChangedEventArgs(e.Name))   
      End Sub
  
  End Class
  
  
  &lt;Serializable()&gt; _    
  Public Class Program
      Inherits MarshalByRefObject
  
      Private _name As String
      Public Event NameChanged(ByVal sender As Object, ByVal e As NameChangedEventArgs)
  
      Public Property Name() As String
          Get
              Return _name
          End Get
          Set(ByVal value As String)
              _name = value
              RaiseEvent NameChanged(Me, New NameChangedEventArgs(_name))
          End Set
      End Property   
  
  End Class
  
  
  &lt;Serializable()&gt; _   
  Public Class NameChangedEventArgs
      Inherits EventArgs
  
      Public Name As String
  
      Public Sub New(ByVal newName As String)
          Name = newName
      End Sub
  
  End Class
  </code></pre>