Click here to Skip to main content
Licence CPOL
First Posted 24 Jan 2009
Views 35,019
Downloads 246
Bookmarked 78 times

A common class for executing tasks with a responsive UI

By | 24 Jan 2009 | Article
Execute actions while making the form wait and still be responsive to other tasks.

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)

About the Author

Paul B.



United States United States

Member

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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralThanx, Pinmemberjohn_max9:44 5 Dec '09  
GeneralHi paul Thanks PinmemberDanie de Kock21:47 27 Jul '09  
GeneralNice mechanism Pinmembercolonel72010:54 23 Jun '09  
GeneralRe: Nice mechanism PinmemberPaul B.11:10 23 Jun '09  
Questionvoid as a return type? Pinmemberuffejz22:23 22 Apr '09  
Hi paul!
 
Is it possible to use your ThreadedExecuter class if the method that is executed returns void?
 
something like this?
 
            using (ThreadedExecuter<void> executer = new ThreadedExecuter<void>(this))
            {
                executer
                    .Process(() =>
                    {
                        foo();
                    })
                    .WhenFinished(null =>
                    {
                        foofoo(); 
                    })
                    .Run();
            }
</void></void>

AnswerRe: void as a return type? [modified] PinmemberDanie de Kock21:46 27 Jul '09  
GeneralRe: void as a return type? Pinmemberuffejz23:04 27 Jul '09  
GeneralRe: void as a return type? PinmemberDanie de Kock23:29 27 Jul '09  
GeneralRe: void as a return type? Sorry PinmemberDanie de Kock23:33 27 Jul '09  
GeneralRe: void as a return type? Sorry Pinmemberuffejz23:40 27 Jul '09  
GeneralRe: void as a return type? Sorry PinmemberDanie de Kock23:46 27 Jul '09  
GeneralRe: void as a return type? Sorry Pinmemberuffejz23:54 27 Jul '09  
GeneralRe: void as a return type? Pinmemberuffejz23:51 27 Jul '09  
AnswerThank you PinmemberDanie de Kock23:53 27 Jul '09  
GeneralRe: Thank you Pinmemberuffejz2:30 28 Jul '09  
GeneralRe: Thank you PinmemberDanie de Kock3:43 28 Jul '09  
GeneralRe: Thank you Pinmemberuffejz4:11 28 Jul '09  
GeneralRe: Thank you PinmemberDanie de Kock4:35 28 Jul '09  
GeneralRe: Thank you Pinmemberuffejz12:17 28 Jul '09  
GeneralRe: Thank you PinmemberDanie de Kock20:27 28 Jul '09  
Generalgood class PinmemberDonsw14:52 19 Feb '09  
GeneralKill thread Pinmembermgrounds4:35 11 Feb '09  
GeneralRe: Kill thread Pinmembermgrounds23:07 11 Feb '09  
GeneralRe: Kill thread Pinmembermgrounds5:54 12 Feb '09  
GeneralRe: Kill thread PinmemberPaul B.3:10 14 Feb '09  

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.

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