Search code examples
wpfweb-servicesmouse-cursor

Avoid hourglass mouse cursor when calling a web-service?


I don't know if this is a general web-services issue or just my specific scenario, which is a WPF application with a DispatcherTimer calling a web-service method. Whenever the timer is called, the mouse cursor automatically changes to an hourglass cursor.

The processing of the call is very short and happens every 3 seconds, so the user experience is that every 3 seconds the mouse filckers as an hourglass for a split-second and then goes back to the normal cursor representation.

How can I avoid this inconvenience?

Thanks.


Solution

  • Is your entire app going unresponsive whenever the timer fires as well, or is the whole process too fast to notice?

    My assumption is that you may be invoking code synchronously on your DispatcherTimer, which could cause brief moments of unresponsiveness (and perhaps the hourglass). To get around this, ensure that your Dispatcher's Tick event is async code.

    Here's a simple little example that, every 3 seconds, performs a second of fake work and then updates the GUI:

    public partial class MainWindow : Window
    {
        private static int foo = 0;
    
        public MainWindow()
        {
            InitializeComponent();
    
            DispatcherTimer timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromMilliseconds(3000);
            timer.Tick += new EventHandler(delegate(object o, EventArgs args)
            {
                StatusBox.Text = "Incrementing";
    
                ThreadStart start = delegate()
                {
                    // Simulate work
                    Thread.Sleep(1000);
    
                    // Update gui
                    this.Dispatcher.BeginInvoke(new Action(delegate
                    {
                        CountingBox.Text = (foo++).ToString();
                        StatusBox.Text = "Waiting";
                    }));
                };
    
                new Thread(start).Start();                
            });
    
            timer.Start();
        }
    }
    

    (There are other methods to accomplish the same goals, this one was simple to rattle off. See the guidance here for a lot more information: http://msdn.microsoft.com/en-us/magazine/cc163328.aspx)