> ## 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;
}
```

此代码与[运行通用任务](/build/core-features/common/async/async-libraries/async-library-xasync-example-run-simple-task)的代码差别不大。
与使用带工作回调的 [XAsyncRun](/reference/system/xasync/functions/xasyncrun) 不同，你在这里调用的是你想使用的 API。异步块的设置方式相同。区别在于 [XUserAddAsync](/reference/system/xuser/functions/xuseraddasync) 会在完成回调中通过 [XUserAddResult](/reference/system/xuser/functions/xuseraddresult) 从调用中检索数据。

## 参考 API 文档

* [XAsync (API 内容)](/reference/system/xasync/xasync_members)
  * 函数
    * [XAsyncRun](/reference/system/xasync/functions/xasyncrun)
* [xuser (API 内容)](/reference/system/xuser/xuser_members)
  * 函数
    * [XUserAddAsync](/reference/system/xuser/functions/xuseraddasync)
    * [XUserAddResult](/reference/system/xuser/functions/xuseraddresult)

## 另请参阅

[XAsync 库概述](/build/core-features/common/async/async-libraries/async-library-xasync)

[设置异步任务（示例）](/build/core-features/common/async/async-libraries/async-library-xasync-example-setup-async-task)

[运行简单任务（示例）](/build/core-features/common/async/async-libraries/async-library-xasync-example-run-simple-task)

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


## Related topics

- [运行简单任务示例](/zh-CN/build/core-features/common/async/async-libraries/async-library-xasync-example-run-simple-task.md)
- [设置异步任务示例](/zh-CN/build/core-features/common/async/async-libraries/async-library-xasync-example-setup-async-task.md)
- [用于异步任务编程的 XAsync 库](/zh-CN/build/core-features/common/async/async-libraries/index.md)
- [如何：使用进程任务队列](/zh-CN/build/core-features/common/async/async-task-queue-design-howto/using-process-task-queue.md)
- [异步编程概述](/zh-CN/build/core-features/common/async/async-toc.md)
