开发者

Wpf windows freeze when other thread starts to do some job

开发者 https://www.devze.com 2023-03-01 02:14 出处:网络
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 Progr开发者_高级运维ess

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 Progr开发者_高级运维essBar 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.


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

精彩评论

暂无评论...
验证码 换一张
取 消