title: Avoiding the woes of Invoke/BeginInvoke in cross-thread WinForm event handling?
body: <p>I'm still plagued by background threading in a WinForm UI.  Why?  Here are some of the issues:</p>
  
  <ol>
  <li>Obviously the most important issue, I can not modify a Control unless I'm executing on the same thread that created it.</li>
  <li>As you know, Invoke, BeginInvoke, etc are not available until after a Control is created.</li>
  <li>Even after RequiresInvoke returns true, BeginInvoke can still throw ObjectDisposed and even if it doesn't throw, it may never execute the code if the control is being destroyed.</li>
  <li>Even after RequiresInvoke returns true, Invoke can indefinitely hang waiting for execution by a control that was disposed at the same time as the call to Invoke.</li>
  </ol>
  
  <p>I'm looking for an elegant solution to this problem, but before I get into specifics of what I'm looking for I thought I would clarify the problem.  This is to take the generic problem and put a more concrete example behind it.  For this example let's say we are transferring larger amounts of data over the internet.  The user interface must be able to show a progress dialog for the transfer already in-progress.  The progress dialog should update constantly and quickly (updates 5 to 20 times per second).  The user can dismiss the progress dialog at any time and recall it again if desired.  And further, lets pretend for arguments sake that if the dialog is visible, it must process every progress event.  The user can click Cancel on the progress dialog and via modifying the event args, cancel the operation.</p>
  
  <p>Now I need a solution that will fit in the following box of constraints:</p>
  
  <ol>
  <li>Allow a worker thread to call a method on a Control/Form and block/wait until execution is complete.</li>
  <li>Allow the dialog itself to call this same method at initialization or the like (and thus not use invoke).</li>
  <li>Place no burden of implementation on the handling method or the calling event, the solution should only change the event subscription itself.</li>
  <li>Appropriately handle blocking invokes to a dialog that might be in the process of disposing.  Unfortunately this is not as easy as checking for IsDisposed.</li>
  <li>Must be able to be used with any event type (assume a delegate of type EventHandler)</li>
  <li>Must not translate exceptions to TargetInvocationException.</li>
  <li>The solution must work with .Net 2.0 and higher</li>
  </ol>
  
  <p>So, can this be solved given the constraints above?  I've searched and dug through countless blogs and discussions and alas I'm still empty handed.</p>
  
  <p>Update: I do realize that this question has no easy answer.  I've only been on this site for a couple of days and I've seen some people with a lot of experience answering questions.  I'm hoping that one of these individuals has solved this sufficiently enough for me to not spend the week or so it will take to build a reasonable solution.</p>
  
  <p>Update #2: Ok, I'm going to try and describe the problem in a little more detail and see what (if anything) shakes out.  The following properties that allow us to determine it's state have a couple of things raise concerns...</p>
  
  <ol>
  <li><p>Control.InvokeRequired = Documented to return false if running on current thread or if IsHandleCreated returns false for all parents.
  I'm troubled by the InvokeRequired implementation having the potential to either throw ObjectDisposedException or potentially even re-create the object's handle.  And since InvokeRequired can return true when we are not able to invoke (Dispose in progress) and it can return false even though we might need to use invoke (Create in progress) this simply can't be trusted in all cases.  The only case I can see where we can trust InvokeRequired returning false is when IsHandleCreated returns true both before and after the call (BTW the MSDN docs for InvokeRequired do mention checking for IsHandleCreated).  </p></li>
  <li><p>Control.IsHandleCreated = Returns true if a handle has been assigned to the control; otherwise, false.
  Though IsHandleCreated is a safe call it may breakdown if the control is in the process of recreating it's handle.  This potential problem appears to be solveable by performing a lock(control) while accessing the IsHandleCreated and InvokeRequired.</p></li>
  <li><p>Control.Disposing = Returns true if the control is in the process of disposing.</p></li>
  <li>Control.IsDisposed = Returns true if the control has been disposed.
  I'm considering subscribing to the Disposed event and checking the IsDisposed property to determin if BeginInvoke will ever complete.  The big problem here is the lack of a syncronization lock durring the Disposing -> Disposed transition.  It's possible that if you subscribe to the Disposed event and after that verify that Disposing == false &amp;&amp; IsDisposed == false you still may never see the Disposed event fire.  This is due to the fact that the implementation of Dispose sets Disposing = false, and then sets Disposed = true.  This provides you an oppertunity (however small) to read both Disposing and IsDisposed as false on a disposed control.  </li>
  </ol>
  
  <p>... my head hurts :(  Hopefully the information above will shed a little more light on the issues for anyone having these troubles.  I appreciate your spare thought cycles on this.</p>
  
  <p>Closing in on the trouble... The following is the later half of the Control.DestroyHandle() method:</p>
  
  <pre><code>if (!this.RecreatingHandle &amp;&amp; (this.threadCallbackList != null))
  {
      lock (this.threadCallbackList)
      {
          Exception exception = new ObjectDisposedException(base.GetType().Name);
          while (this.threadCallbackList.Count &gt; 0)
          {
              ThreadMethodEntry entry = (ThreadMethodEntry) this.threadCallbackList.Dequeue();
              entry.exception = exception;
              entry.Complete();
          }
      }
  }
  if ((0x40 &amp; ((int) ((long) UnsafeNativeMethods.GetWindowLong(new HandleRef(this.window, this.InternalHandle), -20)))) != 0)
  {
      UnsafeNativeMethods.DefMDIChildProc(this.InternalHandle, 0x10, IntPtr.Zero, IntPtr.Zero);
  }
  else
  {
      this.window.DestroyHandle();
  }
  </code></pre>
  
  <p>You'll notice the ObjectDisposedException being dispatched to all waiting cross-thread invocations.  Shortly following this is the call to this.window.DestroyHandle() which in turn destroys the window and set's it's handle reference to IntPtr.Zero thereby preventing further calls into the BeginInvoke method (or more precisely MarshaledInvoke which handle both BeginInvoke and Invoke).  The problem here is that after the lock releases on threadCallbackList a new entry can be inserted before the Control's thread zeros the window handle.  This appears to be the case I'm seeing, though infrequently, often enough to stop a release.</p>
  
  <p>Update #4:</p>
  
  <p>Sorry to keep dragging this on; however, I thought it worth documenting here.  I've managed to solve most of the problems above and I'm narrowing in on a solution that works.  I've hit one more issue I was concerned about, but until now, have not seen 'in-the-wild'.</p>
  
  <p>This issue has to do with the genius that wrote Control.Handle property:</p>
  
  <pre><code>    public IntPtr get_Handle()
      {
          if ((checkForIllegalCrossThreadCalls &amp;&amp; !inCrossThreadSafeCall) &amp;&amp; this.InvokeRequired)
          {
              throw new InvalidOperationException(SR.GetString("IllegalCrossThreadCall", new object[] { this.Name }));
          }
          if (!this.IsHandleCreated)
          {
              this.CreateHandle();
          }
          return this.HandleInternal;
      }
  </code></pre>
  
  <p>This by itself is not so bad (regardless of my opinions on get { } modifications); however, when combined with the InvokeRequired property or the Invoke/BeginInvoke method it is bad.  Here is the basic flow the Invoke:</p>
  
  <pre><code>if( !this.IsHandleCreated )
      throw;
  ... do more stuff
  PostMessage( this.Handle, ... );
  </code></pre>
  
  <p>The issue here is that from another thread I can successfully pass through the first if statement, after which the handle is destroyed by the control's thread, thus causing the get of the Handle property to re-create the window handle on my thread.  This then can cause an exception to be raised on the original control's thread.  This one really has me stumped as there is no way to guard against this.  Had they only use the InternalHandle property and tested for result of IntPtr.Zero this would not be an issue.</p>