当其他线程开始执行某些工作时 Wpf 窗口冻结

发布于 2024-11-03 12:39:08 字数 221 浏览 0 评论 0原文

WPF 中有什么东西可以在 WindowsForms 中执行“DoEvents”方法吗?

我问这个问题是因为我正在尝试在线程中使用 COM Interop,当它执行其工作时,ProgressBar 正在更新。

我找不到任何看起来很容易做到这一点的东西。

我没有太多的时间来阅读和实现一些疯狂的事情,我几乎要退出并让ProgressBar 的属性IsInminated 为True。

is there anything in WPF that do in WindowsForms the method "DoEvents"?

I am asking this because i am trying to use COM Interop in a thread, and when it is doing his job the ProgressBar is being updated.

I can't find anything that seems to be easy to do this.

I don't have too much time to be reading and implementing some crazy things, i am almost quitting and leaving the ProgressBar with the Property IsIndeterminate as True.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我很坚强 2024-11-10 12:39:08

以下示例向您展示如何在 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);

The following example shows you, how to execute some action in another thread than the UI-thread. I don't know a lot about COM, therefore I can not say how this combines with COM-calls, but for the .net-side, this should help you without reading a lot. Here you find the documentation.

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();  

Although I do not recommend to use it, here a code-example that does something like you know from DoEvents:

DispatcherFrame frame = new DispatcherFrame();
Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background, new DispatcherOperationCallback(delegate(object parameter) {
                frame.Continue = false;
                return null;
            }), null);
            Dispatcher.PushFrame(frame);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文