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

# Set up async task

> Learn how to allocate an XAsyncBlock, assign its task queue, context, and completion callback, and why a block can't be shared between active async tasks.

This topic provides an example of how to set up async tasks. Set up an [XAsyncBlock](https://docs.xbox.com/reference/system/xasync/structs/xasyncblock.md) 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)](https://docs.xbox.com/reference/system/xasync/xasync_members.md)
  * Structures
    * [XAsyncBlock](https://docs.xbox.com/reference/system/xasync/structs/xasyncblock.md)

## See also

[XAsync library overview](https://docs.xbox.com/build/core-features/common/async/async-libraries/async-library-xasync.md)

[Run simple task (example)](https://docs.xbox.com/build/core-features/common/async/async-libraries/async-library-xasync-example-run-simple-task.md)

[Run Microsoft Game Development Kit (GDK) API task (example)](https://docs.xbox.com/build/core-features/common/async/async-libraries/async-library-xasync-example-run-gdk-task.md)

[XAsync](https://docs.xbox.com/reference/system/xasync/xasync_members.md)


## Related topics

- [XAsync libraries for asynchronous task programming](https://docs.xbox.com/build/core-features/common/async/async-libraries/index.md)
- [Run simple task](https://docs.xbox.com/build/core-features/common/async/async-libraries/async-library-xasync-example-run-simple-task.md)
- [Run Microsoft Game Development Kit (GDK) API task](https://docs.xbox.com/build/core-features/common/async/async-libraries/async-library-xasync-example-run-gdk-task.md)
- [XAsyncBlock](https://docs.xbox.com/reference/system/xasync/structs/xasyncblock.md)
- [Set up custom provider](https://docs.xbox.com/build/core-features/common/async/async-libraries/async-library-xasyncprovider-example-setup-custom-provider.md)
