> ## 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.

# 非同期タスクのセットアップ例

> 非同期タスクのセットアップ例

このトピックでは、非同期タスクをセットアップする方法の例を示します。実行する非同期タスク用に [XAsyncBlock](/reference/system/xasync/structs/xasyncblock) をセットアップします。これらの非同期ブロックは、複数のアクティブな非同期タスク間で共有できません。ただし、元のタスクが完了した後は、別のタスク用に再利用できます。単純なパターンとしては、呼び出しごとにブロックを動的に割り当て、完了コールバックで呼び出しが終了したときにそれを解放するというものがあります。

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

完了コールバックのパラメーターは、先ほど作成した非同期ブロックです。これにより、必要に応じて、コンテキストにアクセスし、入力と出力の両方として使用するために適切な型にキャストできます。

## リファレンス API ドキュメント

* [XAsync (API 目次)](/reference/system/xasync/xasync_members)
  * 構造体
    * [XAsyncBlock](/reference/system/xasync/structs/xasyncblock)

## 関連項目

[XAsync ライブラリの概要](/build/core-features/common/async/async-libraries/async-library-xasync)

[単純なタスクの実行 (例)](/build/core-features/common/async/async-libraries/async-library-xasync-example-run-simple-task)

[Microsoft Game Development Kit (GDK) API タスクの実行 (例)](/build/core-features/common/async/async-libraries/async-library-xasync-example-run-gdk-task)

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


## Related topics

- [非同期タスク プログラミング向け XAsync ライブラリ](/ja-jp/build/core-features/common/async/async-libraries/index.md)
- [キャンセル可能性のセットアップ例](/ja-jp/build/core-features/common/async/async-libraries/async-library-xasyncprovider-example-setup-cancelability.md)
- [呼び出しメソッドのセットアップ例](/ja-jp/build/core-features/common/async/async-libraries/async-library-xasyncprovider-example-setup-invocation-methods.md)
- [戻りデータのセットアップ例](/ja-jp/build/core-features/common/async/async-libraries/async-library-xasyncprovider-example-setup-return-data.md)
- [単純なタスクの実行例](/ja-jp/build/core-features/common/async/async-libraries/async-library-xasync-example-run-simple-task.md)
