Click here to Skip to main content
Click here to Skip to main content

A common class for executing tasks with a responsive UI

, 24 Jan 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
Execute actions while making the form wait and still be responsive to other tasks.



This is a common way to implement the standard WinForms behavior of executing a potentially long running action while still having the UI be responsive and allowing the user to know that something is happening via the UI. This can be used with standard WinForms, WPF, or an MVP style application which is how I use it.


Some of the other options which I felt weren't as easy as this to use:

Using the code

Download the source and hit F5 - three examples are given:

  • The standard behavior where the UI locks while executing.
  • The new behavior where the UI is not locked and the user can tell the form is busy.
  • What happens in the new behavior when an exception is thrown.

In order to use this, the form or view in question must implement the interface IThreadedExecuterView, in either the form itself or in a base form:

public partial class Form1 : Form, IThreadedExecuterView

#region IThreadedExecuterView Members
public void SetWait(bool isEnabled)
     this.Cursor = (isEnabled ? Cursors.Default : Cursors.WaitCursor);
     button1.Enabled = button2.Enabled = isEnabled;

public void HandleException(Exception ex)
     MessageBox.Show("This is your standard error " + 
                     "handling call here for " + ex.Message);

Below is an example of it in use - there is no need to worry about the UI thread, creating delegates, or doing anything special for exceptions other than handling them in one place.

using (ThreadedExecuter<BusinessObject> executer = 
         new ThreadedExecuter<BusinessObject>(this))
          .Process(() =>
               return GetData(); //executes in background worker
          .WhenFinished(businessObject =>
               UseData(businessObject); //executes on UI thread

It works for value types or reference types. Here is a Unit Test demonstrating its usage:

public void TestThreadedExecuterNormalBehavior()
     int result = 0;
     bool didComplete = false;
     AutoResetEvent waiter = new AutoResetEvent(false);
     IThreadedExecuterView view = 
     using (ThreadedExecuter<int> worker = new ThreadedExecuter<int>(view))
                    return 42;
               .WhenFinished(workerResult => 
                    result = workerResult;
                    didComplete = true;

     waiter.WaitOne(2000, false);
     Assert.AreEqual(42, result);


  • 24-Jan-2009 - Initial version.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Paul B.

United States United States
I've been a software developer since 1996 and have enjoyed C# since 2003. I have a Bachelor's degree in Computer Science and for some reason, a Master's degree in Business Administration. I currently do software development contracting/consulting.

Comments and Discussions

GeneralRe: MVP PinmemberPaul B.25-Jan-09 5:08 
GeneralRe: MVP Pinmemberuffejz25-Jan-09 7:41 
GeneralRe: MVP PinmemberPaul B.25-Jan-09 13:04 
GeneralRe: MVP Pinmemberuffejz26-Jan-09 2:20 
GeneralRe: MVP Pinmemberuffejz29-Jan-09 5:21 
GeneralRe: MVP Pinmemberuffejz19-Feb-09 1:41 
GeneralRe: MVP PinmemberPaul B.19-Feb-09 15:08 
GeneralRe: MVP Pinmemberuffejz23-Feb-09 22:52 
Hi Paul!
I have fixed a new project you can check out.
you can download the file here:[^]
I'm going to use 10 usercontrols in my project.. so it will be 10 timers, feels a lot?! but checkout the code and the text i added to the project and in the pdf file and you will understand what I mean.
GeneralRe: MVP PinmemberPaul B.12-Mar-09 17:26 
GeneralRe: MVP [modified] Pinmemberuffejz15-Mar-09 2:44 
GeneralRe: MVP PinmemberPaul B.15-Mar-09 4:47 
GeneralRe: MVP [modified] Pinmemberuffejz15-Mar-09 5:27 
GeneralRe: MVP PinmemberPaul B.15-Mar-09 13:28 
GeneralRe: MVP [modified] Pinmemberuffejz16-Mar-09 4:02 
GeneralRe: MVP Pinmemberuffejz11-Mar-09 23:58 
GeneralRe: MVP PinmemberPaul B.12-Mar-09 4:11 
GeneralRe: MVP Pinmemberuffejz12-Mar-09 4:25 
GeneralRe: MVP Pinmemberuffejz19-Mar-09 0:48 
GeneralRe: MVP Pinmemberuffejz19-Mar-09 1:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150301.1 | Last Updated 24 Jan 2009
Article Copyright 2009 by Paul B.
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid