Click here to Skip to main content
15,881,803 members
Articles / Programming Languages / C#
Article

WaitCursor hack using using

Rate me:
Please Sign up or sign in to vote.
4.68/5 (19 votes)
2 Mar 2004CPOL1 min read 88.1K   1.3K   17   11
A simple way to display a WaitCursor.

Introduction

A hack to replicate MFC's CWaitCursor class in C#.

Background

I found that the using keyword in C# has two uses:

  1. The using directive, which we all know.
  2. The using statement, which is v. cool. This gave me the idea for this article.

Using the code

Basically, the using statement declares that for a variable, Dispose is always called at the end of the statement block.

This is a good thing. By writing a class that implements IDispose, we can have destructor-like semantics that allow us to clean up after ourselves. This is useful in many scenarios; this article describes one such use.

We will start with our requirements. We want to be able to write code like this:

C#
using ( new CWaitCursor() )
{
    // Do something interesting
}

Just to make things a bit more interesting, we will start by writing a class that displays any System.Windows.Forms.Cursor. It saves the current cursor, displays the specified cursor, then in the Dispose method, it switches back to the saved cursor. This is really quite simple, and looks something like this:

C#
using System.Windows.Forms;

namespace Common
{
    internal class CCursor : IDisposable
    {
        private Cursor saved = null;

        public CCursor( Cursor newCursor )
        {
            saved = Cursor.Current;

            Cursor.Current = newCursor;
        }

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

Our CWaitCursor class just derives from this code, specifying the Cursors.WaitCursor:

C#
internal class CWaitCursor : CCursor
{
    public CWaitCursor() : base( Cursors.WaitCursor ) {}
}

And we're done! The cursor is guaranteed to be cleaned up however we exit the using statement.

Points of Interest

The using statement is good; it should be used everywhere (IMHO) because it gives us some control over disposal of our objects.

I use it a lot in GDI stuff. For example:

C#
using ( Brush brush = new SolidBrush( colour ) )
    Graphics.FillRectangle( brush, rect );

History

Version 1: 2004 March 3.

License

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


Written By
United Kingdom United Kingdom
I discovered C# and .NET 1.0 Beta 1 in late 2000 and loved them immediately.
I have been writing software professionally in C# ever since

In real life, I have spent 3 years travelling abroad,
I have held a UK Private Pilots Licence for 20 years,
and I am a PADI Divemaster.

I now live near idyllic Bournemouth in England.

I can work 'virtually' anywhere!

Comments and Discussions

 
GeneralMy vote of 5 Pin
gstar4211-May-11 10:15
gstar4211-May-11 10:15 
GeneralAnother idea Pin
Maximilian Hänel8-Mar-04 20:19
Maximilian Hänel8-Mar-04 20:19 
The idea is that a lengthy operation is completeted when the user gains back the control over the UI:
public sealed class WaitCursor
{
	readonly Cursor PrevCursor;

	public WaitCursor()
	{
		PrevCursor		= Cursor.Current;
		Cursor.Current	= Cursors.WaitCursor;
		Application.Idle+=new EventHandler(OnIdle);
	}

	public void Restore()
	{
		Cursor.Current	= PrevCursor;
		Application.Idle-=new EventHandler(OnIdle);
	}

	public static void Wait()
	{
		new WaitCursor();
	}

	private void OnIdle(object sender, EventArgs e)
	{
		Restore();
	}
}


In the code you only need to write:
WaitCursor.Wait();

To ensure that the Idle Event is fired right after the long task one could post a dummy message in the WaitCursor Ctor into the message queue. But it doesn't seem to be neccessary since there is usually enough stuff waiting in the queue.

"All languages allow you to write crap code, VB just makes it easier (IMO)."
Michael P Butler

GeneralWait cursor using Pin
GHoffer8-Mar-04 13:16
GHoffer8-Mar-04 13:16 
GeneralRe: Wait cursor using Pin
Nicholas Butler8-Mar-04 19:50
sitebuilderNicholas Butler8-Mar-04 19:50 
GeneralDon't save the old cursor. Pin
Marc Brooks3-Mar-04 11:41
Marc Brooks3-Mar-04 11:41 
GeneralRe: Don't save the old cursor. Pin
Nicholas Butler4-Mar-04 7:44
sitebuilderNicholas Butler4-Mar-04 7:44 
GeneralSomething very similar Pin
Jelle Druyts3-Mar-04 0:34
Jelle Druyts3-Mar-04 0:34 
GeneralRe: Something very similar Pin
Nathan Blomquist3-Mar-04 4:03
Nathan Blomquist3-Mar-04 4:03 
GeneralRe: Something very similar Pin
Jelle Druyts4-Mar-04 8:05
Jelle Druyts4-Mar-04 8:05 
GeneralRe: Something very similar Pin
Nicholas Butler4-Mar-04 7:35
sitebuilderNicholas Butler4-Mar-04 7:35 
GeneralRe: Something very similar Pin
Jelle Druyts4-Mar-04 7:48
Jelle Druyts4-Mar-04 7:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.