title: WPF asynchronous Task<T> blocking UI
body: <p>I already worked with <code>Task</code> type. And all was good while <code>Task</code> return nothing. For example:</p>
  
  <p>XAML:</p>
  
  <pre><code>&lt;Button Name="_button"
          Click="ButtonBase_OnClick"&gt;
          Click
  &lt;/Button&gt;  
  </code></pre>
  
  <p>CodeBehind:</p>
  
  <pre><code>private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
  {
      _button.IsEnabled = false;
  
      Task.Factory.StartNew(() =&gt;
      {
          Thread.Sleep(5*1000);
          Dispatcher.Invoke(new Action(() =&gt; _button.IsEnabled = true));
      });
  }
  </code></pre>
  
  <p>This works fine. But I want to <code>Task</code> returns some value, for example <code>Boolean</code>. So I need to use <code>Task&lt;Boolean&gt;</code>:</p>
  
  <pre><code>private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
  {
      _button.IsEnabled = false;
  
      var task = Task&lt;Boolean&gt;.Factory.StartNew(() =&gt;
      {
          Thread.Sleep(5*1000);
          return true;
      });
  
      if (task.Result)
          _button.IsEnabled = true;
  }
  </code></pre>
  
  <p>And here we have a problem with UI blocking. UI thread is locked untill task will return result.</p>
  
  <pre><code>_button.IsEnabled = false;
  </code></pre>
  
  <p>So, string above is completely ignored. 
  I am on <code>.Net 4.0</code>, so I cannot use <code>async/await</code> approach. 
  This problem really makes me sick... Has it solution?</p>