Loocid
Loocid

Reputation: 6441

WPF Mouse.OverrideCursor Not Working as Documented

I am building a document management system currently and I was trying to change the cursor to a "waiting" cursor while the document is loading, pretty standard.

As per the MSDN documentation, I am using the following code:

 System.Windows.Input.Mouse.OverrideCursor = System.Windows.Input.Cursors.Wait;
 try
 {
     newPage.LoadForm(data);
 }
 finally
 {
     System.Windows.Input.Mouse.OverrideCursor = null;
 }     

The problem is, after LoadForm is finished, the cursor doesn't return to its normal state. I have debugged the program and the "null" line is being run so I have no idea what the problem is.

Any ideas?

Upvotes: 0

Views: 2588

Answers (2)

vesan
vesan

Reputation: 3369

If this is a long-running operation, you might consider moving this whole code to a Task (though in that case you'd have to dispatch the changes to the OverrideCursor property back to the main thread).

I tested this quickly with a Sleep simulating a long-running application and it seemed to work fine (I put this code in the window's constructor in an empty WPF application for testing).

Task.Factory.StartNew(() =>
    {
        Application.Current.Dispatcher.Invoke(() =>
            System.Windows.Input.Mouse.OverrideCursor = System.Windows.Input.Cursors.Wait);
        try
        {
            Thread.Sleep(5000);
        }
        finally
        {
            Application.Current.Dispatcher.Invoke(() =>
                System.Windows.Input.Mouse.OverrideCursor = null);
        }
    });

Upvotes: 3

Ankit
Ankit

Reputation: 672

WORKAROUND

You must set it to the cursor type you want instead of setting it to null. So, instead of setting it to null, set it to Arrow (I assume that is what you would want in Normal state).

So in finally block replace your code with this:

System.Windows.Input.Mouse.OverrideCursor = System.Windows.Input.Cursors.Arrow;

EDIT 1:

Try setting the Cursor to null at the end of try block if in case you do not want to use the workaround.

Upvotes: 0

Related Questions