Home of the AlmostImplementedException

A neat way to work with cursors in WPF using IDisposible

Sometimes its necessary to change the mousecursor of an application to show the user that something goes on. Most times we just want to show the wainting-cursor as long as an operation takes and switch then back.
The first problem you’ll encouter, is exception-handling. You need to put everything into a try-finally-block to get your old mousecursor back after an exception occured.

The following code shows how thats work:

This code works perfectly fine and you can use it where ever you want, but i don’t like to use try-finally. Especially when you have to deal with alot of catch-statements its hard to see why the finally-block is so important.
Since I like (not worship) Clean-Code, I also like the idea of IDisposible and the using-statement.

What would you say about code like this to change the cursor?

Isn’t it beautiful? No try-finally and a clear defined code-block where the cursor should be changed. And it even works with multiple nested cursor-changes!

I found the idea for this in the a stackoverflow-post and improved the code to be more “clean” and useable.

This class is a little bit complicated, because i wanted to use a singleton-pattern. But hey, we all want to improve, so lets have a deeper look.
The first thing we notice is the IDisposable-Interface on Line 1 that is implemented. Here is already a very detailed description on how to use it, so all i say is, that we have to implement the Dispose method in order to use it.
On line 3, you can see a static variable holding an instance of the CursorStack-Class. In combination with the getter “Instance” and the protected constructor, you have a singleton-pattern. The getter is creating a CursorStack instance on the first use and returns the same instance on all following calls. If you don’t know the double question mark, have a look at the null-coalescing – Operator. Line 4 is our Cursor-Stack which will hold the cursors we set until we want to dispose them.
Now all we need is a method which sets the cursor and puts it onto the stack. This is done in UseCursor. The if-statement on line 26 prevents flickering, because we don’t need to set the cursor if its the same.

The method Use is for using this method without an instance of CursorStack. Since this class works as singleton, all calls of Use work on the same instance.

And last but definitely not the least the method Dispose. Here our cursor is removed from the stack when we are finished using it and the cursor is set to it former value. This method is automatically called after the using block is finished.

Now we’ll add one small method and execute an example that shows that nested calls work.

Add this method to the CursorStack-Class. Its a shortcut for setting the waiting-cursor.

And now just execute the following code in a WPF-Application, maybe on a Button-Click to see how the cursor is set and changed back.

Share :

, , , ,

Leave a Reply