Click here to Skip to main content
12,818,050 members (37,953 online)
Click here to Skip to main content
Add your own
alternative version


17 bookmarked
Posted 2 Mar 2004

WaitCursor hack using using

, 2 Mar 2004 CPOL
Rate this:
Please Sign up or sign in to vote.
A simple way to display a WaitCursor.


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


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:

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:

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:

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:

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


Version 1: 2004 March 3.


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


About the Author

Nicholas Butler
United Kingdom United Kingdom

I built my first computer, a Sinclair ZX80, on my 11th birthday in 1980.
In 1992, I completed my Computer Science degree and built my first PC.
I discovered C# and .NET 1.0 Beta 1 in late 2000 and loved them immediately.
I have been writing concurrent software professionally, using multi-processor machines, since 1995.

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.

If you would like help with multithreading, please contact me via my website:

I can work 'virtually' anywhere!

You may also be interested in...


Comments and Discussions

GeneralMy vote of 5 Pin
gstar4211-May-11 11:15
membergstar4211-May-11 11:15 
GeneralAnother idea Pin
Maximilian Hänel8-Mar-04 21:19
memberMaximilian Hänel8-Mar-04 21:19 
GeneralWait cursor using Pin
GHoffer8-Mar-04 14:16
memberGHoffer8-Mar-04 14:16 
GeneralRe: Wait cursor using Pin
Nicholas Butler8-Mar-04 20:50
memberNicholas Butler8-Mar-04 20:50 
GeneralDon't save the old cursor. Pin
Marc Brooks3-Mar-04 12:41
memberMarc Brooks3-Mar-04 12:41 
GeneralRe: Don't save the old cursor. Pin
Nicholas Butler4-Mar-04 8:44
memberNicholas Butler4-Mar-04 8:44 
GeneralSomething very similar Pin
Jelle Druyts3-Mar-04 1:34
memberJelle Druyts3-Mar-04 1:34 
GeneralRe: Something very similar Pin
Nathan Blomquist3-Mar-04 5:03
memberNathan Blomquist3-Mar-04 5:03 
GeneralRe: Something very similar Pin
Jelle Druyts4-Mar-04 9:05
memberJelle Druyts4-Mar-04 9:05 
GeneralRe: Something very similar Pin
Nicholas Butler4-Mar-04 8:35
memberNicholas Butler4-Mar-04 8:35 
GeneralRe: Something very similar Pin
Jelle Druyts4-Mar-04 8:48
memberJelle Druyts4-Mar-04 8:48 
Yes, I am aware that CodeProject has a slightly higher visibility than my blog Wink | ;-)

To respond to your remarks:

1. Not if you're programming asynchronously of course. You could be fetching data in a background thread and put a part of your screen on "hold" using the WaitCursor, while the rest of your app is still responsive and usable. Then again, you wouldn't be able to span an async operation in a single using block of course. You could go for the using block in the worker thread but then you'd have to jump to the GUI thread when setting the control's cursor (since you're not allowed to touch any GUI stuff from another thread). Hmmm maybe I should update my code to make it have a little more sense Smile | :)

2. Whoops, I looked over that, obviously... My bad.

Heh. Party on, dudes Smile | :)

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170308.1 | Last Updated 3 Mar 2004
Article Copyright 2004 by Nicholas Butler
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid