In our haste to finish the coding of a given application, we often overlook great little techniques which can be deployed to ensure best practices in code. And since one of the best ways to learn is by example, we will walk through a very common block of code that is found in almost all Windows based applications – the block which shows and hides a wait cursor when a time consuming task is being executed by the application.

A quick review of event processing in Windows Application tells us that there are 3 different types:

  • Quick Processing (less than a second) – Action performed is executed almost instantaneously without any significant wait period. In this case, you would generally not display any indication to the user about the background task execution. For instance, Open a dialog on the Click of a button.
  • Short Processing (1 second – 5 seconds) – User action consumed a few seconds in execution. In such cases, recommended practice is to show a wait cursor. For instance, Pick records from database to list them in the application screen requested by the user.
  • Long Processing (more than 5 seconds) – Actions which involve a significantly longer processing time, in such cases it is recommended to show progress bar indication to users.

In order to display a wait cursor (category 2 above) the following is the most common block of code seen in most applications:

public object GetData(int id)

{
Cursor.Current = Cursors.WaitCursor;
try
{
// Processing code here
}
finally
{
Cursor.Current = Cursors.Default;
}
}

The code piece above will have to be duplicated at all the places where we want to use wait cursor during processing, this would be a pretty tedious task. Now, let’s have a look at another approach to implement the same feature:

using System;
using System.Windows.Forms;

public class WaitCursor : IDisposable
{
private Cursor cursor;

public WaitCursor()
{
cursor = Cursor.Current;
Cursor.Current = Cursors.WaitCursor;
}

public void Dispose()
{
Cursor.Current = cursor;
}
}

The WaitCursor class above implements IDisposable interface, hence you may use this is the using block. Now see how simple it is to add a wait cursor to your application:

public object GetData(int id)
{
using (new WaitCursor())
{
// Processing code here
}
}

There are some very obvious benefits by doing it this way:

  • It simplifies the code at several places into a single line statement.
  • More importantly, we just ensured that a bug where some developer forgot to restore the cursor to normal can never happen (as long as we have trained our team to simply call the above code).
  • Finally, it is always a best practice to encapsulate repeated code in one place.

One should always be on the lookout for such small coding patterns and implement them.

 

Share and Enjoy

  • Facebook
  • Google Plus
  • Twitter
  • LinkedIn
  • RSS
 

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> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>