开发者

how to give callback function to the Dispatcher.BeginInvoke

开发者 https://www.devze.com 2023-04-03 10:28 出处:网络
I need to use callback function to do some post pr开发者_如何学Goocesing tasks when the function started with the Dispatcher.BeginInvoke finishes. However i couldnot find any parameter in Dispatcher.B

I need to use callback function to do some post pr开发者_如何学Goocesing tasks when the function started with the Dispatcher.BeginInvoke finishes. However i could not find any parameter in Dispatcher.BeginInvoke to accept a callback. Is it possible to give a callback function to Dispatcher.BeginInvoke?


The DispatcherOperation object returned by BeginInvoke has a Completed event on it. Subscribe to that to perform operations upon completion:

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

There's a chance the operation will complete before you subscribe, so you can test the Status property for completion after as well:

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

It's possible for the operation to be aborted as well, so handling/testing for Aborted may also be appropriate.

0

精彩评论

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