I’ve got a standard WPF MainWindow class, and what I’d like is to show a message box using System.Windows.MessageBox, get a response from the user and then run a long running operation (simulated below by a call to Sleep(...)). I’d like to set the cursor to Cursors.Wait before the operation, and back to normal at the end. Here’s what I’ve got:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void ui_button_Click(object sender, RoutedEventArgs e)
{
if (MessageBox.Show("Do you want to change the background?", "Change background", MessageBoxButton.YesNo) == MessageBoxResult.No)
{
return;
}
Cursor = Cursors.Wait;
Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
{
System.Threading.Thread.Sleep(1500);
if (Background != Brushes.Green)
{
Background = Brushes.Green;
}
else
{
Background = Brushes.White;
}
Cursor = Cursors.Arrow;
}));
}
}
This doesn’t work: the cursor never appears as the wait cursor. However, if I comment out the MessageBox lines, it does work. What’s going on here, and how can I get it to work as intended?
The following code works for me: instead of
try this:
You turn off the wait cursor the opposite way: