当其他线程开始做一些工作时,Wpf窗口冻结
本文关键字:Wpf 窗口 冻结 工作 线程 其他 开始 | 更新日期: 2023-09-27 17:52:44
有什么在WPF做在WindowsForms的方法"DoEvents"?
我问这个,因为我试图在一个线程中使用COM互操作,当它正在做他的工作时,ProgressBar正在更新。
我找不到任何容易做到这一点的方法。
我没有太多的时间去阅读和实现一些疯狂的事情,我几乎要退出并离开属性IsIndeterminate为True的ProgressBar
下面的示例向您展示了如何在ui线程之外的另一个线程中执行某些操作。我不太了解COM,因此我不能说它是如何与COM调用结合在一起的,但对于。net方面,这应该可以帮助您不需要阅读很多内容。在这里你可以找到文档。
BackgroundWorker bgWorker = new BackgroundWorker() { WorkerReportsProgress=true};
bgWorker.DoWork += (s, e) => {
// As your requested, here an example on how you yould instantiate your working class,
// registering to some progresse event and relay the progress to the backgorund-worker:
YourClass workingInstance=new YourClass();
workingInstance.WorkProgress+=(o,yourProgressEvent)=>{
bgWorker.ReportProgress(yourProgressEvent.ProgressPercentage);
};
workingInstance.Execute();
};
bgWorker.ProgressChanged+=(s,e)=>{
// Here you will be informed about progress and here it is save to change/show progress.
// You can access from here savely a ProgressBars or another control.
};
bgWorker.RunWorkerCompleted += (s, e) => {
// Here you will be informed if the job is done.
// Use this event to unlock your gui
};
bgWorker.RunWorkerAsync();
虽然我不建议使用它,这里有一个代码示例,它做了一些你从DoEvents中知道的事情:
DispatcherFrame frame = new DispatcherFrame();
Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background, new DispatcherOperationCallback(delegate(object parameter) {
frame.Continue = false;
return null;
}), null);
Dispatcher.PushFrame(frame);