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

# 執行 Microsoft Game Development Kit API 工作範例

> 執行 Microsoft Game Development Kit API 工作範例

本主題提供如何執行 Microsoft Game Development Kit (GDK) API 工作的範例。非同步 Microsoft Game Development Kit (GDK) API 函式會在內部實作工作回呼。不過，它仍會依照非同步區塊中所指定的工作佇列指示來執行。
執行這些方法需要非同步區塊，以及
API 所需的任何參數。

```c++ theme={null}
// Set up the async block and completion callback.
XAsyncBlock* async = new XAsyncBlock{};
async->queue = taskQueue;
async->context = nullptr;
async->callback = [](XAsyncBlock* async)
{
    XUserHandle newUser = nullptr;
    HRESULT result = XUserAddResult(async, &newUser);
    // Handle the completion callback.
    delete async;
};

// Start the async call.
HRESULT hr = XUserAddAsync(XUserAddOptions::None, async);
if (FAILED(hr))
{
    delete async;
}
```

此程式碼與[執行一般工作](/zh-TW/build/core-features/common/async/async-libraries/async-library-xasync-example-run-simple-task)的程式碼差異不大。
您不是搭配工作回呼使用 [XAsyncRun](/zh-TW/reference/system/xasync/functions/xasyncrun)，而是呼叫
您想要使用的 API。非同步區塊的設定方式
相同。差別在於 [XUserAddAsync](/zh-TW/reference/system/xuser/functions/xuseraddasync)
具有要從呼叫中擷取的資料，這會在
完成回呼中透過 [XUserAddResult](/zh-TW/reference/system/xuser/functions/xuseraddresult) 完成。

## 參考 API 文件

* [XAsync (API 內容)](/zh-TW/reference/system/xasync/xasync_members)
  * 函式
    * [XAsyncRun](/zh-TW/reference/system/xasync/functions/xasyncrun)
* [xuser (API 內容)](/zh-TW/reference/system/xuser/xuser_members)
  * 函式
    * [XUserAddAsync](/zh-TW/reference/system/xuser/functions/xuseraddasync)
    * [XUserAddResult](/zh-TW/reference/system/xuser/functions/xuseraddresult)

## 另請參閱

[XAsync 程式庫概觀](/zh-TW/build/core-features/common/async/async-libraries/async-library-xasync)

[設定非同步工作 (範例)](/zh-TW/build/core-features/common/async/async-libraries/async-library-xasync-example-setup-async-task)

[執行簡單工作 (範例)](/zh-TW/build/core-features/common/async/async-libraries/async-library-xasync-example-run-simple-task)

[XAsync](/zh-TW/reference/system/xasync/xasync_members)


## Related topics

- [运行 Microsoft Game Development Kit API 任务示例](/zh-CN/build/core-features/common/async/async-libraries/async-library-xasync-example-run-gdk-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)
- [Microsoft Game Development Kit API 작업 실행 예제](/ko/build/core-features/common/async/async-libraries/async-library-xasync-example-run-gdk-task.md)
- [Microsoft Game Development Kit API タスクの実行例](/ja-jp/build/core-features/common/async/async-libraries/async-library-xasync-example-run-gdk-task.md)
- [Ejemplo de ejecución de una tarea de la API del Microsoft Game Development Kit](/es/build/core-features/common/async/async-libraries/async-library-xasync-example-run-gdk-task.md)
