title: Executing on a background thread using BeginInvoke
body: <p>I'm trying out multithreading but I cant figure out why the the piece of code below blocks the UI when task.Execute() is called?  </p>
  
  <pre><code>public class Task&lt;TRes&gt;
  {
      private Func&lt;TRes&gt; _func;
      public Task(Func&lt;TRes&gt; func)
      {
          _func = func;
      }
      public TRes Execute()
      {
          var iasync = _func.BeginInvoke(null, null);
          return _func.EndInvoke(iasync);
      }
  }
  </code></pre>
  
  <p>I'm not trying to solve any problem in particular. Just wondering why the UI blocks when it hits Thread.Sleep even though the current thread (the one that was put to "sleep") is a ThreadPool thread (and not the main UI thread).  Below is a button click event that uses the Task class. </p>
  
  <pre><code> var task = new Task&lt;string&gt;(() =&gt;
              {
                  var start = DateTime.Now;
                  var isBackGround = Thread.CurrentThread.IsBackground ;
                  var isThreadPool = Thread.CurrentThread.IsBackground;
  
                  Thread.Sleep(5000);
                  return String.Format("Start {0} : End {1}", start, DateTime.Now);
              }
            );
          this.label1.Text = task.Execute();
  </code></pre>