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

# 執行 Microsoft Game Development Kit (GDK) API 工作

> 了解如何使用 XAsyncBlock 呼叫 XUserAddAsync 等非同步 GDK API，然後在完成回呼中使用 XUserAddResult 擷取其資料。

本主題提供如何執行 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;
}
```

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

## 參考 API 文件

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

## 另請參閱

[XAsync 程式庫概觀](https://docs.xbox.com/zh-TW/build/core-features/common/async/async-libraries/async-library-xasync.md)

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

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

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


## Related topics

- [執行簡單工作範例](https://docs.xbox.com/zh-TW/build/core-features/common/async/async-libraries/async-library-xasync-example-run-simple-task.md)
- [設定非同步工作範例](https://docs.xbox.com/zh-TW/build/core-features/common/async/async-libraries/async-library-xasync-example-setup-async-task.md)
- [XAsyncRun](https://docs.xbox.com/zh-TW/reference/system/xasync/functions/xasyncrun.md)
- [XUserAddResult](https://docs.xbox.com/zh-TW/reference/system/xuser/functions/xuseraddresult.md)
- [XUserAddAsync](https://docs.xbox.com/zh-TW/reference/system/xuser/functions/xuseraddasync.md)
