如何给回调函数的Dispatcher.BeginInvoke [英] how to give callback function to the Dispatcher.BeginInvoke
问题描述
我需要使用回调函数做一些后期处理。第任务时的功能开始与Dispatcher.BeginInvoke完成。但是我无法找到任何Dispatcher.BeginInvoke参数接受一个回调。是否有可能得到一个回调函数来Dispatcher.BeginInvoke?
I need to use callback function to do some post procesing 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?
推荐答案
按的BeginInvoke返回的
有一个的DispatcherOperation
对象已完成就可以了
事件。订阅该完成时执行操作:
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) { ... }
这是可能的操作被放弃为好,这样处理/测试中止
也可能是合适的。
这篇关于如何给回调函数的Dispatcher.BeginInvoke的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!