views:

21

answers:

1

Hi everyone!

I need only to show a custom control (a clock with rotating hands) and with this to replace the mouse cursor, the problem is that if I write:

Me.gridScreen.Visibility = Visibility.Visible
' some operations that takes about 1 second
Me.gridScreen.Visibility = Visibility.Hidden
(gridScreen is the grid that contains the user-control)

Obviously I can see nothing, because the update of the UI happens at the end of the procedure. I have tried Me.UpdateLayout(), but it doesn't work.

I have tryed to use the dispacker in many way but none that works :-(

This is my lost attempt:

(uCurClock is the usercontrol, gridScreen a Grid placed at the top-level in the window, with trasparent background, that contains the usercontrol)

Private Sub showClock()
    Dim thread = New System.Threading.Thread(AddressOf showClockIntermediate)
    thread.Start()
End Sub

Private Sub hideClock()
    Dim thread = New System.Threading.Thread(AddressOf hideClockIntermediate)
    thread.Start()
End Sub

Private Sub showClockIntermediate()
    Me.Dispatcher.BeginInvoke(DispatcherPriority.Normal, _
        New Action(AddressOf showClockFinale))
End Sub

Private Sub hideClockIntermediate()
    Me.Dispatcher.BeginInvoke(DispatcherPriority.Normal, _
        New Action(AddressOf hideClockFinale))
End Sub

Private Sub showClockFinale()
    Dim pt As Point = Mouse.GetPosition(Nothing)
    Me.uCurClock.Margin = New Thickness(pt.X - 9, pt.Y - 9, 0, 0)
    Me.gridScreen.Visibility = Visibility.Visible
    Me.Cursor = Cursors.None
    Me.UpdateLayout()
End Sub

Private Sub hideClockFinale()
    Me.gridScreen.Visibility = Visibility.Hidden
    Me.Cursor = Cursors.Arrow
    Me.UpdateLayout()
End Sub

Private Sub u_MouseMove(ByVal sender As System.Object, _
    ByVal e As MouseEventArgs) Handles gridScreen.MouseMove

    Dim pt As Point = e.GetPosition(Nothing)
    Me.uCurClock.Margin = New Thickness(pt.X - 9, pt.Y - 9, 0, 0)

    e.Handled = True
End Sub

Private Sub u_MouseEnter(ByVal sender As System.Object, _
    ByVal e As MouseEventArgs) Handles gridScreen.MouseEnter

    Me.uCurClock.Visibility = Visibility.Visible

    e.Handled = True
End Sub

Private Sub u_MouseLeave(ByVal sender As System.Object, _
    ByVal e As MouseEventArgs) Handles gridScreen.MouseLeave

    Me.uCurClock.Visibility = Visibility.Hidden

    e.Handled = True
End Sub

PIleggi

+1  A: 

The problem is not to do with the composition of messages being executed on the dispatcher, it's that you're executing long-running work on the dispatcher at all. You must ensure that long-running/potentially blocking operations are executed on a background thread. The easiest way to do so is with the BackgroundWorker component.

Excuse the C#:

var backgroundWorker = new BackgroundWorker();

backgroundWorker.DoWork += delegate
{
    // long running work goes here
};
backgroundWorker.RunWorkerCompleted += delegate
{
    // change cursor back to normal here
};

// change cursor to busy here

// kick off the background task
backgroundWorker.RunWorkerAsync();

HTH,
Kent

Kent Boogaart
Thank you, but then I know there aren't solutions. Infact in many case I have procedures with meny operations in whitch I retrive data from UI and others in witch I modify the data of the collections source of UI controls. These operations are forbidden from background threads and it's too hard to do a separation. I think I will only change the mouse cursor...
pileggi
@Kent Boogaart: Thank you very much! I know the BackgroundWorker but I have the problem that it's too hard to do the separation in my methods (that already exist) between long processes and and operations on the UI. The only way, for the moment is to change only the mouse cursor: Me.Cursor = New Cursor("absolute path of .ani file")
pileggi