WaitCursor

By Fons Sonnemans, posted on
1654 Views

For a long time I thought that I only had to set the Cursor.Current to a WaitCursor before a long running operation, the .NET runtime would reset it back to the Default cursor. Turns out that this is only true when the mouse is moved. Bummer.

// Cursor.Current are automatically reset to Default when the
// mouse is moved and the application is idle!
Cursor.Current = Cursors.WaitCursor;

The solution for this problem is very easy. I created a helper class called WaitCursor which set the Cursor.Current and restores it to the original value when it it disposed.

public sealed class WaitCursor : IDisposable {   &nbsp   &nbsp   &nbsp   &nbsp
   &nbspprivate Cursor _prev;

   &nbsppublic WaitCursor(){
   &nbsp   &nbsp_prev= Cursor.Current;
   &nbsp   &nbspCursor.Current = Cursors.WaitCursor;
   &nbsp}

   &nbsppublicvoid Dispose(){
   &nbsp   &nbspCursor.Current =_prev;
   &nbsp}
}

I create the instance of the WairCursor class inside a using statement. This will automatically call the Dispose() method when it goes out of scope.

using ( new WaitCursor()){
   &nbsp// long running operation, for example:
   &nbspSystem.Threading.Thread.Sleep(1000);
}

Afterwards I did some searching around on the web. Turns out I'm not the first to come up with this solution. Charlie Poole already posted it a few years ago. Another bummer.

All postings/content on this blog are provided "AS IS" with no warranties, and confer no rights. All entries in this blog are my opinion and don't necessarily reflect the opinion of my employer or sponsors. The content on this site is licensed under a Creative Commons Attribution By license.

Leave a comment

Blog comments

0 responses