Multi-Threaded WinForms Application – Basics

Have you ever written a WinForms application that froze when a button was pushed? Have your users complained that the application is slow (or locking up their machine)? Do you want your applications to be responsive and user friendly? If so, then you will want to read this three part series on Writing Multi-Threaded WinForms Applications.In the first part of this series, I will discuss the benefits of using threads in WinForm applications and show you an example of an application that does not use threads. This will allow us to revisit this application through out the series (Part 2 – BackgroundWorker, Part 3 – MVP and Part 4 – Dynamic Proxies) discussing different ways to inject threads into your WinForms applications.What are the benefits to using threads in a WinForms application?

  1. Perception, to your users, that operations complete faster. By using multiple threads, the users will perceive that the operations complete faster, even though they actually may take longer. The reason for this is feedback. Using threads allows us to give the users feedback while the operation is in progress. This might be in the form of a progress bar, moving image or anything else that means operation in progress.
  2. Multiple tasks can be in progress at the same time. An example of this might be auto saving your users work without forcing them to stop what they are doing. Many applications do this today: all good word processors, Outlook and many others.
  3. GUI remains responsive during the operation. This allows us to perform basic tasks while we are performing work for the user. Without threads, our GUI will not even minimize during a long running task. This can cause our users to wait until the task complete before they can even use their computer again.
  4. Operations can be cancelled. Canceling a long running operation gives the user a chance to change their mind.
  5. GUIs can report Feedback. Feedback is the single most important reason for using threads in a WinForm application. This tells the user that their action did do something. It can also give the user an idea about how much longer a task is going to take.

Example GUI Without Threads

public class Model{    private const int MILLIS_IN_A_SECOND = 1000;    public string LongRunningMethod()    {        Thread.Sleep(8 * MILLIS_IN_A_SECOND);        return "I am done";    }}public partial class MainForm : Form{    void Button1Click(object sender, EventArgs e)    {        label.Text = new Model().LongRunningMethod();    }}

After running the MainForm class, click on the Perform Operation button. After clicking on the button, the GUI will be unresponsive for the 8 seconds that model sleeps during the LongRunningMethod method call. You can verify that the GUI is unresponsive by clicking on the minimize button. The Form will not minimize until the LogRunningMethod has completed.This is an example of a case where using a thread will allow the GUI to continue to be responsive while the model is working. In the next part of this series, I will show how using a thread will make the GUI more responsive during a long running model operation.f you would like to see the full source for the above example, check it out from the following subversion repository. you have any questions or comments, please feel free to drop me a note.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: