title: Sync over Async avoiding deadlock and prevent UI from being responsive
body: <p>We have a library that is being used by WPF and/or Winforms clients.</p>
  
  <p>We've provided an asynchronous method similar to:</p>
  
  <pre><code>Task&lt;int&gt; GetIntAsync()
  </code></pre>
  
  <p>We've also (unfortunately) provided a synchronous wrapper method:</p>
  
  <pre><code>int GetInt();
  </code></pre>
  
  <p>which essentially just calls the asynchronous method and calls <code>.Result</code> on its task.  </p>
  
  <p>We recently realized under certain circumstances some code in the <code>GetIntAsync</code> needs to run on the main UI thread (it needs to use a legacy COM component that is marked as "Single" Threading model (i.e. the component must run in the main STA thread not just any STA thread)</p>
  
  <p>So the problem is that when <code>GetInt()</code> is called on the main thread, it will <strong>deadlock</strong> since </p>
  
  <ul>
  <li>the <code>.Result</code> blocks the main thread, </li>
  <li>the code within <code>GetIntAsync()</code> uses <code>Dispatcher.Invoke</code> to attempt to run on the main thread.</li>
  </ul>
  
  <p>The synchronous method is already being consumed so it would be a breaking change to remove it.  So instead, we've opted for using the <a href="http://blogs.msdn.com/b/pfxteam/archive/2010/05/04/10007499.aspx">WaitWithPumping</a> in our synchronous <code>GetInt()</code> method to allow the invoking to the main thread to work.</p>
  
  <p>This works fine except for clients that use <code>GetInt()</code> from their UI code.  Previously, they expected that using <code>GetInt()</code> would leave their UI unresponsive--that is, if they called <code>GetInt()</code> from within a button's click event handler, they would expect that no windows messages were processed until the handler returned.  Now that messages are pumped, their UI <strong>is</strong> responsive and that same button can be clicked again (and they probably didn't code their handler to be re-entrant).</p>
  
  <p>If there is a reasonable solution, we'd like to not have our clients need to code against the UI being responsive during a call to <code>GetInt</code></p>
  
  <p><strong>Question:</strong></p>
  
  <ul>
  <li>Is there a way to do a <code>WaitWithPumping</code> that will pump "Invoke to main" messages, but not pump other UI related messages?</li>
  <li>It would suffice for our purposes if the client UI behaved as if a modal dialog were currently shown, albeit hidden (i.e. user couldn't access the other windows).  But, from what I read, you cannot hide a modal dialog.</li>
  <li>Other workarounds you can think of would be appreciated.</li>
  </ul>