title: All COM events blocked after BackgroundWorker completes (within AppDomain)
body: <p>I have a program that handles multiple instances of a class, each class having its own instance of a DLL COM object.  In order to make sure each class uses a separate instance, I create the class with an AppDomain:</p>
  
  <pre><code>_objectDomain = AppDomain.CreateDomain("somename")
  Dim item As ObjectType
  item = CType(_objectDomain.CreateInstanceAndUnwrap(GetType(ObjectType).Assembly.FullName, _
  GetType(ObjectType).FullName), ObjectType)
  'do a bunch of stuff to item
  _itemList.Add(item)
  </code></pre>
  
  <p>Later on I initialize the connection used within the COM object:</p>
  
  <pre><code>For Each obj In _itemList
      obj.Connect()
  Next
  </code></pre>
  
  <p>And each Connect does:</p>
  
  <pre><code>Private Sub Connect()
      Try
          _item.Connect("SQL", TableName)
          If Not _item.Initialized Then _
              Throw New Exception("Connection was not initialized.")
          _item.StartMirroring() 'this watches specified files to see if they've been updated
      Catch Ex As Exception
          Alert("Could not connect to database.")
      End Try
  End Sub
  </code></pre>
  
  <p>While the AppDomains run in parallel, each obj.connect() call waits for _item.Connect() to finish before moving on, so the initialization can take some time if I have multiple items. So I try to use BackgroundWorker to make them run concurrently:</p>
  
  <pre><code>Public Sub Connect()
      _itemWorker = New BackgroundWorker
      AddHandler _temWorker.DoWork, AddressOf ConnectWorker
      AddHandler _itemWorker.RunWorkerCompleted, AddressOf StartMirror
      _itemWorker.RunWorkerAsync()
  End Sub
  </code></pre>
  
  <p>However, if I do it this way events from the _item no longer fire after RunWorkerCompleted is fired.  The .Connect() function raises many events, all of which fire successfully within and without the BackgroundWorker, but once I get to StartMirror events are no longer raised from the COM (I should get a MirrorStateChanged event when I call .StartMirroring(), but nothing happens). If I take out the backgroundworker and let them go in sequence they are raised no problem. I can raise events within my class just fine in either case.</p>
  
  <p>According to <a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker%28v=vs.100%29.aspx" rel="nofollow">this link</a>: </p>
  
  <blockquote>
    <p>BackgroundWorker events are not marshaled across AppDomain boundaries. Do not use a BackgroundWorker component to perform multithreaded operations in more than one AppDomain.</p>
  </blockquote>
  
  <p>However, I do not believe that to be an issue here, for two reasons:</p>
  
  <ol>
  <li>The BackgroundWorker is contained within the AppDomain for that instance of the class, and does not communicate with any other AppDomain except the main thread (through events)</li>
  <li>The events still fire within the BackgroundWorker, but not once it finishes</li>
  </ol>
  
  <p>I have tried re-associating the handlers after the Worker ends and creating the BackgroundWorker at the main application level, both with no success. Why would the COM object stop raising events? I can still interact with it just fine, but unfortunately the (third-patry, closed-source) COM communicates primarily through events, so I can't just ping it to ask if it's actively checking the mirrors.  If I have to do large processes in sequence that's okay, because it's going to have very limited use, but I'd much prefer to run those in the background if at all possible.</p>