How to provide a Dispatcher.BeginInvoke callback function

I need to use a callback function to perform some postprocessor tasks when the function starts with Dispatcher.BeginInvoke. However, I could not find any parameter in Dispatcher.BeginInvoke to accept the callback. Can I enable callback function in Dispatcher.BeginInvoke?

+8
c # callback wpf dispatcher
source share
1 answer

The DispatcherOperation object returned by BeginInvoke has a Completed event on it. Subscribe to this to complete operations after completion:

 var dispatcherOp = Dispatcher.BeginInvoke( /* your method here */); dispatcherOp.Completed += (s, e) => { /* callback code here */ }; 

The likelihood that the operation will complete before signing, so you can check the Status property to complete after:

 if (dispatcherOp.Status == DispatcherOperationStatus.Completed) { ... } 

It is also possible that the operation will be aborted, so processing / testing for Aborted may also be appropriate.

+10
source share

All Articles