title: Can I create a thread that can modify the user interface, and that I can abort?
body: <p>I have a lengthy user-interface operation on my form which is triggered whenever an event is fired.  Rather than have the UI block while the operation takes place, I'd like to perform the operation in another thread, and abort that thread and start again if the event fires again.</p>
  
  <p>However, to safely alter controls on my form, I need to use the form's Invoke or BeginInvoke methods.  If I do that, then I could put all my UI operations in one function like this:</p>
  
  <pre><code>private delegate void DoUIStuffDelegate(Thing1 arg1, Thing2 arg2);
  private void doUIStuff(Thing1 arg1, Thing2 arg2)
  {
      control1.Visible = false;
      this.Controls.Add(arg1.ToButton());
      ...
      control100.Text = arg2.ToString();
  }
  
  ...
  
  private void backgroundThread()
  {
      Thing1 arg1 = new Thing1();
      Thing2 arg2 = new Thing2();
  
      this.Invoke(new DoUIStuffDelegate(doUIStuff), arg1, arg2);
  }
  
  Thread uiStuffThread = null;
  
  public void OnEventFired()
  {
      if (uiStuffThread != null)
          uiStuffThread.Abort();
  
      uiStuffThread = new Thread(backgroundThread);
      uiStuffThread.Start();
  }
  </code></pre>
  
  <p>but if I do that, then I lose the benefit of working in a separate thread.  Alternatively, I could put them each in their own function like this:</p>
  
  <pre><code>private delegate void DoUIStuffLine1Delegate();
  private delegate void DoUIStuffLine2Delegate(Thing1 arg1);
  ...
  
  private delegate void DoUIStuffLine100Delegate(Thing2 arg2);
  
  private void doUIStuffLine1()
  {
      control1.Visible = false;
  }
  
  private void doUIStuffLine2()
  {
      this.Controls.Add(arg1.ToButton());
  }
  
  ...
  
  private void doUIStuffLine100(Thing2 arg2)
  {
      control100.Text = arg2.ToString();
  }
  
  ...
  
  private void backgroundThread()
  {
      Thing1 arg1 = new Thing1();
      Thing2 arg2 = new Thing2();
  
      this.Invoke(new DoUIStuffLine1Delegate(doUIStuffLine1));
      this.Invoke(new DoUIStuffLine2Delegate(doUIStuffLine2), arg1);
      ...
      this.Invoke(new DoUIStuffLine100Delegate(doUIStuffLine100), arg2);
  }
  
  Thread uiStuffThread = null;
  
  public void OnEventFired()
  {
      if (uiStuffThread != null)
          uiStuffThread.Abort();
  
      uiStuffThread = new Thread(backgroundThread);
      uiStuffThread.Start();
  }
  </code></pre>
  
  <p>but that's a horrible, unmaintainable mess.  Is there a way to create a thread that can modify the user interface, and that I can abort?  So that I can just do something like this:</p>
  
  <pre><code>private void doUIStuff()
  {
      Thing1 arg1 = new Thing1();
      Thing2 arg2 = new Thing2();
  
      control1.Visible = false;
      this.Controls.Add(arg1.ToButton());
      ...
      control100.Text = arg2.ToString();
  }
  
  Thread uiStuffThread = null;
  
  public void OnEventFired()
  {
      if (uiStuffThread != null)
          uiStuffThread.Abort();
  
      uiStuffThread = this.GetNiceThread(doUIStuff);
      uiStuffThread.Start();
  }
  </code></pre>
  
  <p>without having to disable cross-thread checks on my form?  Ideally I'd like to be able to set some attribute on the thread or the method which individually wrapped all of the operations in delegates that then got invoked on the form's thread.</p>