Asynchronous tasks on Winform

The problem

When you create winform applications, doing tasks in the background is essential to avoid user frustation. Unfortunatly, it could make you write a lot more code. Here are some utility methods that reduce the complexity of asynchronous calls within windows forms.

Within you base class

You probably have a common base class for all your UI components. Add these methods :

Within your UI classes

Now the only thing you need to do is to encapsulate the methode content with the Async() method :

The traditional way

Just in case you didn’t get it. This is what you should NOT DO:

Happy coding !

3 thoughts on “Asynchronous tasks on Winform”

  1. How could this be modified to allow the user to automatically cancel the previous async call and start a new async call (with new params) by clicking the button again?

  2. Hi Mark,

    This could be achieved with the following :
    – create a static dictionary containing all current calls
    – AsyncCall is a class containing :
    * the caller (a Control eg: the button)
    * the thread that executes the code

    In the Async() method, add a new instance of call at the begining and remove it at the end.

    If there is already a call with the same caller, kill its thread, remove it from the collection and do a normal call.

    Was it clear enough ?

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">