WPF Wait Cursor With BackgroundWorker Thread
Asked Answered
L

2

3

I want to show the hourglass cursor and disable the window while a BackgroundWorker process runs in another thread.

This is what I'm doing:

Private Sub MyButton_Click(...)
    Dim box As New AnotherWpfWindow()
    box.Owner = Me
    ...
    box.ShowDialog()
    If (box.DialogResult.GetValueOrDefault = True) Then
        Me.IsEnabled = False
        Me.Cursor = Cursors.Wait
        MyBackgroundWorker.RunWorkerAsync()
    End If
End Sub

Private Sub MyBackgroundWorker_RunWorkerCompleted(...)
    UpdateInterface()
    Me.IsEnabled = True
    Me.Cursor = Cursors.Arrow
End Sub

The window becomes disabled like I want, but the cursor remains an arrow. How can I make it the Wait cursor?

It seems to work for vg1890 according to this question: Disabling all but one control in a WPF window

Ludhiana answered 17/4, 2009 at 22:7 Comment(0)
I
8

What seems to be happening here is that WPF is ignoring the Cursor setting on the disabled window. The following workaround seems to work: instead of disabling the window itself, disable the content of the window:

C#:

((UIElement)Content).IsEnabled = false;
Cursor = Cursors.Wait;

// and in RunWorkerCompleted handler:
((UIElement)Content).IsEnabled = true;
Cursor = Cursors.Arrow;

Visual Basic:

DirectCast(Content, UIElement).IsEnabled = False
Cursor = Cursors.Wait

' and in RunWorkerCompleted handler:'
DirectCast(Content, UIElement).IsEnabled = True
Cursor = Cursors.Arrow
Inca answered 17/4, 2009 at 22:26 Comment(0)
O
11

Another way is to change the cursor globally, is..

Mouse.OverrideCursor = Cursors.Wait;

//Do something
//...

Mouse.OverrideCursor = null;
Oxeyed answered 21/11, 2011 at 11:36 Comment(0)
I
8

What seems to be happening here is that WPF is ignoring the Cursor setting on the disabled window. The following workaround seems to work: instead of disabling the window itself, disable the content of the window:

C#:

((UIElement)Content).IsEnabled = false;
Cursor = Cursors.Wait;

// and in RunWorkerCompleted handler:
((UIElement)Content).IsEnabled = true;
Cursor = Cursors.Arrow;

Visual Basic:

DirectCast(Content, UIElement).IsEnabled = False
Cursor = Cursors.Wait

' and in RunWorkerCompleted handler:'
DirectCast(Content, UIElement).IsEnabled = True
Cursor = Cursors.Arrow
Inca answered 17/4, 2009 at 22:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.