Click here to Skip to main content
13,800,491 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


10 bookmarked
Posted 20 Dec 2010
Licenced CPOL

Hourglass Mouse Cursor Always Changes Back to its Original Image. How?

Rate this:
Please Sign up or sign in to vote.
IDisposable with “using” block helps in all cases
Testing and debugging UI code, I am often faced with such a situation, when the mouse cursor is changed to Hourglass, but not changed back.
Sometimes, it happened to my own code as well.

The mouse cursor is supposed to be changed back to its original or default image after the procedure which could cause a delay if UI response is already completed (or failed). Sometimes it is not changed back because this procedure fails due to exception or some other reason. Sometimes the code used to set mouse cursor to its original image is simply forgotten.

As a result, I came to a simple pattern which helps to avoid such situation in all cases. I used it in several different UI systems. Now I'll show how it is implemented with .NET. The idea is using helper class implementing IDisposable.

This is a WPF implementation written in C#:

namespace SA.Universal.UI {
    using IDisposable = System.IDisposable;
    using FrameworkElement = System.Windows.FrameworkElement;
    using Cursor = System.Windows.Input.Cursor;
    using Cursors = System.Windows.Input.Cursors;
    using Debug = System.Diagnostics.Debug;

    public class WaitCursorIndicator : IDisposable {

        public WaitCursorIndicator(FrameworkElement owner) {
            this.Onwer = owner;
                owner != null,
                "WaitCursorIndicator expects non-null argument");
            if (owner == null) return;
            Previous = owner.Cursor;
            owner.Cursor = Cursors.Wait;
        } //WaitCursorIndicator

        void IDisposable.Dispose() {
            if (this.Onwer == null) return;
            this.Onwer.Cursor = Previous;
        } //IDisposable.Dispose

        FrameworkElement Onwer;
        Cursor Previous;

    } //class WaitCursorIndicator

} //namespace SA.Universal.UI

With System.Windows.Forms, it is implemented in a very similar way:

namespace SA.Universal.UI {
    using System.Windows.Forms;
    using IDisposable = System.IDisposable;
    using Debug = System.Diagnostics.Debug;

    public class WaitCursorIndicator : System.IDisposable {

        public WaitCursorIndicator(Control owner) {
            this.Owner = owner;
                owner != null,
                "WaitCursorIndicator expects non-null argument");
            if (owner == null) return;
            Previous = this.Owner.Cursor;
            owner.Cursor = Cursors.WaitCursor;
        } //WaitCursorIndicator

        void IDisposable.Dispose() {
            if (Owner == null) return;
            Owner.Cursor = Previous;
        } //IDisposable.Dispose

        Control Owner;
        Cursor Previous;

    } //class WaitCursorIndicator

} //namespace SA.Universal.UI

The usage looks identical for WPF and System.Windows.Forms:

using(new WaitCursorIndicator(owner)) {
   //... some long-running code here
} //end using

In this code snippet, owner is most typically a form or a window.

This way, mouse cursor is changed back no matter what, even if an exception is thrown.
On exit from the "using" block, WaitCursorIndicator.Dispose is always called. In fact, using this "using" block is just syntactic sugar functionally strictly equivalent to the try-finally block with Dispose called in the finally section.


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


About the Author

Sergey Alexandrovich Kryukov
United States United States
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralTo DaveAuld: I did not undestand this: "Never head of things... Pin
Sergey Alexandrovich Kryukov19-Dec-10 8:05
mvpSergey Alexandrovich Kryukov19-Dec-10 8:05 
GeneralYou're right, of course, but in real life people make mistak... Pin
Sergey Alexandrovich Kryukov19-Dec-10 8:00
mvpSergey Alexandrovich Kryukov19-Dec-10 8:00 
GeneralI always thought that if a code changed the mouse pointer, t... Pin
DaveAuld18-Dec-10 22:25
protectorDaveAuld18-Dec-10 22:25 

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 | Cookies | Terms of Use | Mobile
Web01 | 2.8.181214.1 | Last Updated 21 Dec 2010
Article Copyright 2010 by Sergey Alexandrovich Kryukov
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid