Share via


Set up return data example

This topic provides an example of how to set up return data. If there's custom return data, support it by using the provider and a custom method so that you can retrieve the created data.

// Provider Callback
[](XAsyncOp op, const XAsyncProviderData* providerData)
{
    switch(op)
    {
    case XAsyncOp::GetResult:
        memcpy(providerData->buffer, &context->result, sizeof(uint64_t));
        break;

    case XAsyncOp::DoWork:
        context->result = 12345678;
        XAsyncComplete(providerData->async, S_OK, sizeof(uint64_t));
        break;

    // Other cases..
    }
}

As explained in the setup example, the provider must first add support for return data by specifying a buffer size in XAsyncComplete and filling it out in the GetResult case. After support is added, add a method to retrieve the result for the caller.

HRESULT CustomProviderMethodAsyncResult(XAsyncBlock* async, uint64_t* outResult)
{
    return XAsyncGetResult(async, CustomProviderMethodAsync,
        sizeof(uint64_t), outResult, nullptr);
}

You can call XAsyncGetResult, but the previous method creates a readable pairing between the custom provider method. It automatically converts to the data type that's needed for the async result. This method is also safe to use within any completion callback to gather results from the work.

See also

XAsyncProvider library overview
Set up custom provider (example)
Setup invocation methods (example)
Set up cancelability (example)
XAsyncProvider