Click here to Skip to main content
11,496,146 members (590 online)
Click here to Skip to main content

A common class for executing tasks with a responsive UI

, 24 Jan 2009 CPOL 52.2K 538 82
Execute actions while making the form wait and still be responsive to other tasks.
The site is currently in read-only mode for maintenance. Posting of new items will be available again shortly.

1.jpg

Introduction

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.

Background

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))
{
     executer
          .Process(() =>
          {
               return GetData(); //executes in background worker
          })
          .WhenFinished(businessObject =>
          {
               UseData(businessObject); //executes on UI thread
          })
     .Run();
}

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

[Test]
public void TestThreadedExecuterNormalBehavior()
{
     int result = 0;
     bool didComplete = false;
     AutoResetEvent waiter = new AutoResetEvent(false);
     IThreadedExecuterView view = 
        MockRepository.GenerateStub<IThreadedExecuterView>();
     using (ThreadedExecuter<int> worker = new ThreadedExecuter<int>(view))
     {
          worker
               .Process(()=>
               {
                    Thread.Sleep(1000);
                    return 42;
               })
               .WhenFinished(workerResult => 
               {
                    result = workerResult;
                    didComplete = true;
                    waiter.Set();
               })
          .Run();
     }

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

History

  • 24-Jan-2009 - Initial version.

License

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

Share

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

 
GeneralThanx, Pin
john_max5-Dec-09 10:44
memberjohn_max5-Dec-09 10:44 
GeneralHi paul Thanks Pin
Danie de Kock27-Jul-09 22:47
memberDanie de Kock27-Jul-09 22:47 
GeneralNice mechanism Pin
colonel72023-Jun-09 11:54
membercolonel72023-Jun-09 11:54 
GeneralRe: Nice mechanism Pin
Paul B.23-Jun-09 12:10
memberPaul B.23-Jun-09 12:10 
Questionvoid as a return type? Pin
uffejz22-Apr-09 23:23
memberuffejz22-Apr-09 23:23 
AnswerRe: void as a return type? [modified] Pin
Danie de Kock27-Jul-09 22:46
memberDanie de Kock27-Jul-09 22:46 
GeneralRe: void as a return type? Pin
uffejz28-Jul-09 0:04
memberuffejz28-Jul-09 0:04 
GeneralRe: void as a return type? Pin
Danie de Kock28-Jul-09 0:29
memberDanie de Kock28-Jul-09 0:29 
GeneralRe: void as a return type? Sorry Pin
Danie de Kock28-Jul-09 0:33
memberDanie de Kock28-Jul-09 0:33 
GeneralRe: void as a return type? Sorry Pin
uffejz28-Jul-09 0:40
memberuffejz28-Jul-09 0:40 
GeneralRe: void as a return type? Sorry Pin
Danie de Kock28-Jul-09 0:46
memberDanie de Kock28-Jul-09 0:46 
GeneralRe: void as a return type? Sorry Pin
uffejz28-Jul-09 0:54
memberuffejz28-Jul-09 0:54 
GeneralRe: void as a return type? Pin
uffejz28-Jul-09 0:51
memberuffejz28-Jul-09 0:51 
AnswerThank you Pin
Danie de Kock28-Jul-09 0:53
memberDanie de Kock28-Jul-09 0:53 
GeneralRe: Thank you Pin
uffejz28-Jul-09 3:30
memberuffejz28-Jul-09 3:30 
GeneralRe: Thank you Pin
Danie de Kock28-Jul-09 4:43
memberDanie de Kock28-Jul-09 4:43 
GeneralRe: Thank you Pin
uffejz28-Jul-09 5:11
memberuffejz28-Jul-09 5:11 
GeneralRe: Thank you Pin
Danie de Kock28-Jul-09 5:35
memberDanie de Kock28-Jul-09 5:35 
GeneralRe: Thank you Pin
uffejz28-Jul-09 13:17
memberuffejz28-Jul-09 13:17 
GeneralRe: Thank you Pin
Danie de Kock28-Jul-09 21:27
memberDanie de Kock28-Jul-09 21:27 
Generalgood class Pin
Donsw19-Feb-09 15:52
memberDonsw19-Feb-09 15:52 
GeneralKill thread Pin
mgrounds11-Feb-09 5:35
membermgrounds11-Feb-09 5:35 
GeneralRe: Kill thread Pin
mgrounds12-Feb-09 0:07
membermgrounds12-Feb-09 0:07 
GeneralRe: Kill thread Pin
mgrounds12-Feb-09 6:54
membermgrounds12-Feb-09 6:54 
GeneralRe: Kill thread Pin
Paul B.14-Feb-09 4:10
memberPaul B.14-Feb-09 4:10 
GeneralRe: Kill thread Pin
mgrounds16-Feb-09 0:47
membermgrounds16-Feb-09 0:47 
GeneralRe: Kill thread Pin
Paul B.16-Feb-09 5:39
memberPaul B.16-Feb-09 5:39 
GeneralRe: Kill thread Pin
uffejz22-Apr-09 23:35
memberuffejz22-Apr-09 23:35 
GeneralRe: Kill thread Pin
Danie de Kock29-Jul-09 0:06
memberDanie de Kock29-Jul-09 0:06 
GeneralRe: Kill thread Pin
Danie de Kock29-Jul-09 0:24
memberDanie de Kock29-Jul-09 0:24 
GeneralUnit Test Code Fails Pin
Henrik Jonsson27-Jan-09 10:06
memberHenrik Jonsson27-Jan-09 10:06 
GeneralRe: Unit Test Code Fails Pin
Paul B.27-Jan-09 16:31
memberPaul B.27-Jan-09 16:31 
GeneralRe: Unit Test Code Fails Pin
Henrik Jonsson28-Jan-09 9:26
memberHenrik Jonsson28-Jan-09 9:26 
GeneralMVP usage Pin
Paul B.26-Jan-09 5:23
memberPaul B.26-Jan-09 5:23 
GeneralMy vote of 2 Pin
ProJester125-Jan-09 9:03
memberProJester125-Jan-09 9:03 
GeneralRe: My vote of 2 Pin
Paul B.25-Jan-09 12:57
memberPaul B.25-Jan-09 12:57 
GeneralRe: My vote of 2 Pin
Paul B.25-Jan-09 13:00
memberPaul B.25-Jan-09 13:00 
GeneralRe: My vote of 2 Pin
Jon_Boy20-May-09 10:39
memberJon_Boy20-May-09 10:39 
GeneralMVP Pin
uffejz25-Jan-09 4:04
memberuffejz25-Jan-09 4:04 
GeneralRe: MVP Pin
Paul B.25-Jan-09 5:08
memberPaul B.25-Jan-09 5:08 
GeneralRe: MVP Pin
uffejz25-Jan-09 7:41
memberuffejz25-Jan-09 7:41 
GeneralRe: MVP Pin
Paul B.25-Jan-09 13:04
memberPaul B.25-Jan-09 13:04 
GeneralRe: MVP Pin
uffejz26-Jan-09 2:20
memberuffejz26-Jan-09 2:20 
GeneralRe: MVP Pin
uffejz29-Jan-09 5:21
memberuffejz29-Jan-09 5:21 
GeneralRe: MVP Pin
uffejz19-Feb-09 1:41
memberuffejz19-Feb-09 1:41 
GeneralRe: MVP Pin
Paul B.19-Feb-09 15:08
memberPaul B.19-Feb-09 15:08 
GeneralRe: MVP Pin
uffejz23-Feb-09 22:52
memberuffejz23-Feb-09 22:52 
GeneralRe: MVP Pin
Paul B.12-Mar-09 17:26
memberPaul B.12-Mar-09 17:26 
GeneralRe: MVP [modified] Pin
uffejz15-Mar-09 2:44
memberuffejz15-Mar-09 2:44 
GeneralRe: MVP Pin
Paul B.15-Mar-09 4:47
memberPaul B.15-Mar-09 4:47 

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.150520.1 | Last Updated 24 Jan 2009
Article Copyright 2009 by Paul B.
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid