title: BackgroundWorker OnWorkCompleted throws cross-thread exception
body: <p>I have a simple UserControl for database paging, that uses a controller to perform the actual DAL calls. I use a <code>BackgroundWorker</code> to perform the heavy lifting, and on the <code>OnWorkCompleted</code> event I re-enable some buttons, change a <code>TextBox.Text</code> property and raise an event for the parent form.</p>
  
  <p>Form A holds my UserControl. When I click on some button that opens form B, even if I don't do anything "there" and just close it, and try to bring in the next page from my database, the <code>OnWorkCompleted</code> gets called on the worker thread (and not my Main thread), and throws a cross-thread exception.</p>
  
  <p>At the moment I added a check for <code>InvokeRequired</code> at the handler there, but isn't the whole point of <code>OnWorkCompleted</code> is to be called on the Main thread? Why wouldn't it work as expected?</p>
  
  <p>EDIT:</p>
  
  <p>I have managed to narrow down the problem to arcgis and <code>BackgroundWorker</code>. I have the following solution wich adds a Command to arcmap, that opens a simple <code>Form1</code> with two buttons.</p>
  
  <p>The first button runs a <code>BackgroundWorker</code> that sleeps for 500ms and updates a counter.
  In the <code>RunWorkerCompleted</code> method it checks for <code>InvokeRequired</code>, and updates the title to show whethever the method was originaly running inside the main thread or the worker thread.
  The second button just opens <code>Form2</code>, which contains nothing.</p>
  
  <p>At first, all the calls to <code>RunWorkerCompletedare</code> are made inside the main thread (As expected - thats the whold point of the RunWorkerComplete method, At least by what I understand from the <a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker(VS.80).aspx" rel="nofollow noreferrer">MSDN</a> on <code>BackgroundWorker</code>)</p>
  
  <p>After opening and closing <code>Form2</code>, the <code>RunWorkerCompleted</code> is always being called on the worker thread. I want to add that I can just leave this solution to the problem as is (check for <code>InvokeRequired</code> in the <code>RunWorkerCompleted</code> method), but I want to understand why it is happening against my expectations. In my "real" code I'd like to always know that the <code>RunWorkerCompleted</code> method is being called on the main thread.</p>
  
  <p>I managed to pin point the problem at the <code>form.Show();</code> command in my <code>BackgroundTesterBtn</code> - if I use <code>ShowDialog()</code> instead, I get no problem (<code>RunWorkerCompleted</code> always runs on the main thread). I do need to use <code>Show()</code> in my ArcMap project, so that the user will not be bound to the form.</p>
  
  <p>I also tried to reproduce the bug on a normal WinForms project. I added a simple project that just opens the first form without ArcMap, but in that case I couldn't reproduce the bug - the <code>RunWorkerCompleted</code> ran on the main thread, whether I used <code>Show()</code> or <code>ShowDialog()</code>, before and after opening <code>Form2</code>. I tried adding a third form to act as a main form before my <code>Form1</code>, but it didn't change the outcome.</p>
  
  <p><a href="http://depositfiles.com/files/1cu4o0493" rel="nofollow noreferrer">Here</a> is my simple sln (VS2005sp1) - it requires</p>
  
  <p>ESRI.ArcGIS.ADF(9.2.4.1420)</p>
  
  <p>ESRI.ArcGIS.ArcMapUI(9.2.3.1380)</p>
  
  <p>ESRI.ArcGIS.SystemUI (9.2.3.1380)</p>