Partager via


Run simple task example

This topic provides an example of how to run a simple user task. Simple tasks can be started with the XAsyncRun function. This function takes the async block as an input and then requires implementing the work callback as a functor or lambda input. Remember that the async block specifies the task queue to use and the optional completion callback.

XAsyncBlock* async = new XAsyncBlock{};
async->queue = taskQueue;
async->context = contextData;
async->callback = 
    [](XAsyncBlock* async)
    {
        // The optional completion callback.
        delete async;
    }

XAsyncRun(async,
    [](XAsyncBlock* async)->HRESULT
    {
        // The work callback is implemented here.
        return S_OK;
    });

This example uses XAsyncRunto start a custom, simple asynchronous task. The async block contains the task queue to use and the completion callback. XAsyncRun requires a work callback, and it has a return value. It's returned from a call to XAsyncGetStatus afterward. Internally, XAsyncRun has a simple async provider setup to handle the enqueuing of the specified work callback, the enqueuing of the completion callback in the async block, and other state changes for the process. This means that no further code is needed for running simple async tasks—just set up the async block and call XAsyncRun.

See also

XAsync library overview

Set up async task (example)

Run Microsoft Game Development Kit (GDK) API task (example)

XAsync