如何给回调函数的Dispatcher.BeginInvoke [英] how to give callback function to the Dispatcher.BeginInvoke

查看:748
本文介绍了如何给回调函数的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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆