Click here to Skip to main content
Click here to Skip to main content

WaitCursor hack using using

By , 2 Mar 2004
 

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:

    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 );

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)

About the Author

Nicholas Butler
United Kingdom United Kingdom
Member

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!

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5membergstar4211 May '11 - 10:15 
GeneralAnother ideamemberMaximilian Hänel8 Mar '04 - 20:19 
GeneralWait cursor usingmemberGHoffer8 Mar '04 - 13:16 
GeneralRe: Wait cursor usingmemberNicholas Butler8 Mar '04 - 19:50 
GeneralDon't save the old cursor.memberMarc Brooks3 Mar '04 - 11:41 
GeneralRe: Don't save the old cursor.memberNicholas Butler4 Mar '04 - 7:44 
GeneralSomething very similarmemberJelle Druyts3 Mar '04 - 0:34 
GeneralRe: Something very similarmemberNathan Blomquist3 Mar '04 - 4:03 
Not to be picky, but in FireFox your page formatting gets all messed up near the bottom. This makes reading the source code of your version of the wait cursor hard. In IE it looks fine...
 

-Nathan
 
---------------------------
Hmmm... what's a signature?
GeneralRe: Something very similarmemberJelle Druyts4 Mar '04 - 8:05 
GeneralRe: Something very similarmemberNicholas Butler4 Mar '04 - 7:35 
GeneralRe: Something very similarmemberJelle Druyts4 Mar '04 - 7:48 

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

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