tags:

views:

62

answers:

2

For a view constructed using WPF, I want to change the mouse cursor to a hourglass when the application is busy and nonresponsive.

One solution is to add

 this.Cursor = Cursors.Wait;

to all the places that may cause the UI to become non-responsive. But obviously this is not the best solution. I am wondering what is the best way to achive this?

Is it possible to achive this by using Styles or Resources?

Thanks,

+1  A: 

The best way would be to not cause the UI to become non-responsive ever, offloading all of the work to other threads/tasks as appropriate.

Other than that, you're kindof in a catch-22: if you did add a way to detect that the ui is non-responsive, there's no good way to change the cursor, as the place you'd need to do that (the even thread) is non-responsive... You might be able to pinvoke out to standard win32 code to change the cursor for the whole window, though?

Otherwise, you'd have to do it pre-emptively, like your question suggests.

John Gardner
+6  A: 

We did a disposable class that changes the cursor for us when the app is going to take long, it looks like this:

public class WaitCursor : IDisposable
{
    private Cursor _previousCursor;

    public WaitCursor()
    {
        _previousCursor = Mouse.OverrideCursor;

        Mouse.OverrideCursor = Cursors.Wait;
    }

    #region IDisposable Members

    public void Dispose()
    {
        Mouse.OverrideCursor = _previousCursor;
    }

    #endregion
}

And we use it like this:

using(new WaitCursor())
{
    // very long task
}

Might not be the greatest design, but it does the trick =)

Carlo
Well done on using IDisposable! A good way to make sure we always return to the previous cursor.
Xavier Poinas