如何为IAsyncOperation指定一个回调方法
How to specify a callback method for IAsyncOperation
是否可以指定在完成async
操作后调用的方法?
平台:c++, Windows Phone 8
我需要实现异步发送UDP数据包的非阻塞方法。它们有我的方法:
onWriteComplete(int errorCode)
在操作完成时被回调。
这是我尝试过的:
res = await asyncWrite();
onWriteComplete( res );
异步操作在Windows Phone 8和Windows RT应用程序的所有语言中都以类似的方式工作。异步操作返回一个IAsyncOperation结果,你可以用它来链接一个函数,让它在操作完成后运行。
在c++中,你可以用类似c#的create_task和task::then函数来创建任务并链接它们。查看c++中异步编程(Windows Store Apps)的示例。
示例从IAsyncOperation结果创建一个任务,并安排另一个任务在第一个任务完成时执行:
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
相关文章:
- 重写另一个方法 [C++] 使用的超类回调函数
- 使用一个回调函数从多个订阅者分配信息
- C++中嵌套在另一个 LinkedList 中的 LinkedList 整数上的回调函数问题
- 一个类的方法作为回调
- 通过回调功能,打破一个循环的最佳方法是什么?
- Delphi 如何在另一个函数中使用回调函数作为参数
- 在用户模式下从另一个进程回调,没有额外的线程
- 给出一个方法作为回调函数(gcc 4.7.1)
- 一个回调函数应该如何更新/返回值
- 将方法作为回调从一个类传递到另一个类
- 如何将方法作为回调传递给另一个类
- 设置一个回调函数,该函数是类的非静态成员函数
- C++,pthread和静态回调。 "this" 返回一个指针,指向派生的基类(第 2 部分)
- c++定时器实现-将一个成员函数分配给信号回调函数指针
- 如何使一个回调全局,所以我可以使用它的其他功能
- Std::ostream,为每行调用一个回调
- 我如何做一个回调从FireBreath到JavaScript
- DirectShow从内存中读取一个回调方法
- 创建一个回调结构传递给LuaJIT FFI
- 如何为IAsyncOperation指定一个回调方法