Is it possible to specify a method to be called after finishing an async
operation?
Platform : C++, Windows Phone 8
I need to implement non-blocking method for sending UDP packets asynchronously. And them have my method:
onWriteComplete(int errorCode)
called back when the operation completes.
Here's what I've tried:
res = await asyncWrite();
onWriteComplete( res );
But no luck.
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.
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.
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