C# 当其他线程开始执行某些任务时,Wpf窗口冻结

C# 当其他线程开始执行某些任务时,Wpf窗口冻结,c#,wpf,multithreading,C#,Wpf,Multithreading,WPF中是否有任何东西可以在Windows窗体中执行方法“DoEvents” 我这样问是因为我试图在一个线程中使用COM互操作,当它在执行他的工作时,ProgressBar正在更新 我找不到任何容易做到的事情 我没有太多的时间来阅读和实现一些疯狂的事情,我几乎要退出并留下ProgressBar,属性IsIndestime为True。下面的示例向您展示了如何在UI线程以外的其他线程中执行某些操作。我对COM了解不多,因此我不能说它是如何与COM调用结合在一起的,但是对于.net方面,这应该可以帮助

WPF中是否有任何东西可以在Windows窗体中执行方法“DoEvents”

我这样问是因为我试图在一个线程中使用COM互操作,当它在执行他的工作时,ProgressBar正在更新

我找不到任何容易做到的事情


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

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

您确实意识到在WinForms中也不应该使用
DoEvents
,对吗?在单独的线程上执行其他任务。像这样长时间运行的任务永远不属于UI线程,不管是WPF还是WinForms。我如何向ProgressChanged事件报告进度?@Miguel:我不知道你的情况,但也许你可以向这个类添加一个报告进度的事件?然后被叫人可以注册参加这个活动。你能举个例子来说明你的建议吗?@Miguel:我把我的例子改了一点。希望这能引导您找到解决方案。。。