如何为IAsyncOperation指定回调方法 [英] How to specify a callback method for IAsyncOperation
问题描述
是否可以指定在完成 async
操作之后要调用的方法?
Is it possible to specify a method to be called after finishing an async
operation?
平台:C ++,Windows Phone 8
Platform : C++, Windows Phone 8
我需要实现非阻塞方法来异步发送UDP数据包.他们有我的方法:
I need to implement non-blocking method for sending UDP packets asynchronously. And them have my method:
onWriteComplete(int errorCode)
操作完成后回叫.
这是我尝试过的:
res = await asyncWrite();
onWriteComplete( res );
但是没有运气.
推荐答案
Windows Phone 8和Windows RT应用程序中的所有语言之间的异步操作都以类似的方式工作.异步操作返回IAsyncOperation结果,您可以使用该结果链接函数以在操作完成后运行.
Asynchronous operations work in similar ways across all languages in Windows Phone 8 and Windows RT applications. The asynchronous operation returns an IAsyncOperation result which you can use to chain a function to run when the operation completes.
在C ++中,您可以使用 create_task 和 task:then 函数.检查 C ++中的异步编程(Windows应用商店商店)一个例子.
In C++ you can create tasks and chain them in a way similar to C# using the create_task and task::then functions. Check Asynchronous Programming in C++ (Windows Store Apps) for an example.
该示例根据IAsyncOperation结果创建一个任务,并安排另一个任务在第一个任务完成时执行:
The sample creates a task from an IAsyncOperation result and schedules another task to execute when the first task completes:
auto deviceEnumTask = create_task(deviceOp);
// Call the task’s .then member function, and provide
// the lambda to be invoked when the async operation completes.
deviceEnumTask.then( [this] (DeviceInformationCollection^ devices )
{
for(int i = 0; i < devices->Size; i++)
{
DeviceInformation^ di = devices->GetAt(i);
// Do something with di...
}
}); // end lambda
这篇关于如何为IAsyncOperation指定回调方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!