> ## Documentation Index
> Fetch the complete documentation index at: https://devdocs.xbox.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Set up async task example

> Set up async task example

This topic provides an example of how to set up async tasks. Set up an [XAsyncBlock](/reference/system/xasync/structs/xasyncblock) for the async task that will run.
These async blocks can't be shared between multiple active async tasks.
However, they can be reused for a different task after the original task is finished. A
simple pattern is to just dynamically allocate the block for each call,
and then release it when the call is finished in the completion callback.

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

The parameter for the completion callback is the async block that was previously created. This allows for the context to be accessed and cast to the proper type for usage as both input and output, if you want to do so.

## Reference API documentation

* [XAsync (API contents)](/reference/system/xasync/xasync_members)
  * Structures
    * [XAsyncBlock](/reference/system/xasync/structs/xasyncblock)

## See also

[XAsync library overview](/build/core-features/common/async/async-libraries/async-library-xasync)

[Run simple task (example)](/build/core-features/common/async/async-libraries/async-library-xasync-example-run-simple-task)

[Run Microsoft Game Development Kit (GDK) API task (example)](/build/core-features/common/async/async-libraries/async-library-xasync-example-run-gdk-task)

[XAsync](/reference/system/xasync/xasync_members)


## Related topics

- [XAsync libraries for asynchronous task programming](/build/core-features/common/async/async-libraries/index.md)
- [Run simple task example](/build/core-features/common/async/async-libraries/async-library-xasync-example-run-simple-task.md)
- [Run Microsoft Game Development Kit API task example](/build/core-features/common/async/async-libraries/async-library-xasync-example-run-gdk-task.md)
- [Clean up task queue example](/build/core-features/common/async/async-libraries/async-library-xtaskqueue-example-cleanup-task-queue.md)
- [Set up cancelability example](/build/core-features/common/async/async-libraries/async-library-xasyncprovider-example-setup-cancelability.md)
