title: Can BeginInvoke interrupt code already running on the UI thread?
body: <p>Suppose I have some code which is running in the UI thread, which spawns a new thread to do work in the background, and then goes on to do UI work.  When the background work is done, BeginInvoke is called from the new thread.  If the UI work is still going on, will the callback given to BeginInvoke interrupt the UI work, or will it wait?</p>
  
  <p>Code example - add a button called button1 to a form and add button1_Click to its click event, and then add this to the form:</p>
  
  <pre><code>bool continueWork = true;
  
  private void button1_Click(object sender, EventArgs e)
  {
      Thread workThread = new Thread(performBackgroundWork);
  
      workThread.IsBackground = true;
      workThread.SetApartmentState(ApartmentState.STA);
      workThread.Start();
  
      for (long i = 0; i &lt; long.MaxValue; i++)
      {
          if (!continueWork)
              break;
  
          button1.Text = i.ToString();
      }
  }
  
  private void performBackgroundWork()
  {
      Thread.Sleep(1);
      button1.BeginInvoke(new MethodInvoker(stopUIWork));            
  }
  
  private void stopUIWork()
  {
      continueWork = false;
  }
  </code></pre>
  
  <p>What is button1's text after it is clicked?</p>