我正在作为后台进程运行一个长进程,因为该进程使我的UI没有响应。
现在的问题是,当进程作为后台进程运行时,我想显示一个等待光标。我尝试使用dispatcher来更新光标,但它不起作用。
我的流程在点击按钮时运行:
private void btnStartAsyncOperation_Click(object sender, EventArgs e)
{
backgroundworkerprocess.RunWorkerAsync();
}
在backgroundworkerprocess事件中:
void backgroundworkerprocess_DoWork(object sender, DoWorkEventArgs e)
{
this.dispatcher.invoke(DispatcherPriority.Background,
new action((delegate) {
this.cursor = cursors.wait
})
);
}
我希望这会导致等待光标显示在表单上的任何位置,但只有当鼠标放在按钮上时才会显示。
private void btnStartAsyncOperation_Click(object sender, EventArgs e)
{
this.Cursor = Cursors.WaitCursor;
backgroundworkerprocess.RunWorkerAsync();
}
void backgroundworkerprocess_DoWork(object sender, DoWorkEventArgs e)
{
// do work
}
然后只需在RunWorkerCompleted事件中将Cursor改回即可。
我认为人们不理解你。
请尝试此操作,将其发送到Dispatcher:Window.GetWindow(this).Cursor=Cursors.WaitCursor;