我只需要显示一个自定义控件(带旋转指针的时钟)并用此替换鼠标光标,问题是如果我写:
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)
显然我什么也看不见,因为UI的更新发生在程序的最后。我尝试过Me.UpdateLayout(),但它不起作用。
我试图以多种方式使用调度程序,但没有一个可行: - (
这是我失败的尝试:
(uCurClock是用户控件,gridScreen是一个放置在窗口顶层的网格,背景为trasparent,包含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
答案 0 :(得分:1)
问题与调度程序上正在执行的消息的组成无关,而是你在调度程序上执行长时间运行的工作。您必须确保在后台线程上执行长时间运行/可能阻塞的操作。最简单的方法是使用BackgroundWorker
组件。
请原谅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();