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

# XAsyncBegin

> XAsyncBegin

# XAsyncBegin

初始化一个 [XAsyncBlock](https://docs.xbox.com/zh-CN/reference/system/xasync/structs/xasyncblock.md) 并通过异步提供程序开始异步工作。

## Syntax

```cpp theme={null}
HRESULT XAsyncBegin(  
         XAsyncBlock* asyncBlock,  
         void* context,  
         const void* identity,  
         const char* identityName,  
         XAsyncProvider* provider  
)  
```

### Parameters

*asyncBlock*   \_Inout\_\
类型：[XAsyncBlock\*](https://docs.xbox.com/zh-CN/reference/system/xasync/structs/xasyncblock.md)

指向保存异步调用数据的 [XAsyncBlock](https://docs.xbox.com/zh-CN/reference/system/xasync/structs/xasyncblock.md) 的指针。

*context*   \_In\_opt\_\
类型：void\*

可选。要存储在传递给 [XAsyncProvider](https://docs.xbox.com/zh-CN/reference/system/xasyncprovider/functions/xasyncprovider.md) 回调函数的 [XAsyncProviderData](https://docs.xbox.com/zh-CN/reference/system/xasyncprovider/structs/xasyncproviderdata.md) 对象中的上下文指针。

*identity*   \_In\_opt\_\
类型：void\*

可选。指向可用于标识异步调用的任意标记的指针。

*identityName*   \_In\_opt\_\
类型：char\*

可选。命名提供异步调用的函数的字符串。此参数的值通常设置为 `__FUNCTION__` 编译器宏。

*provider*   \_In\_\
类型：[XAsyncProvider](https://docs.xbox.com/zh-CN/reference/system/xasyncprovider/functions/xasyncprovider.md)\*

指向要为异步调用调用的 [XAsyncProvider](https://docs.xbox.com/zh-CN/reference/system/xasyncprovider/functions/xasyncprovider.md) 回调函数的指针。

### Return value

类型：[HRESULT](https://learn.microsoft.com/openspecs/windows_protocols/ms-erref/0642cb2f-2075-4469-918c-4441e69c548a)

成功时返回 `S_OK`；否则，返回错误代码。有关错误代码列表，请参阅[错误代码](https://docs.xbox.com/zh-CN/reference/errorcodes.md)。

## Remarks

调用 `XAsyncBegin` 以指示异步提供程序应开始异步工作。调用 `XAsyncBegin` 后，其他 [XAsyncProvider](https://docs.xbox.com/zh-CN/reference/system/xasyncprovider/xasyncprovider_members.md) 函数（例如 [XAsyncGetResult](https://docs.xbox.com/zh-CN/reference/system/xasyncprovider/functions/xasyncgetresult.md)）将提供有意义的数据。

调用 `XAsyncBegin` 时，将调用 *provider* 中指定的回调函数，并在 [XAsyncProvider](https://docs.xbox.com/zh-CN/reference/system/xasyncprovider/functions/xasyncprovider.md) 的 *op* 参数中指定 [XAsyncOp::Begin](https://docs.xbox.com/zh-CN/reference/system/xasyncprovider/enums/xasyncop.md)。如果回调函数实现了此操作代码，它应通过调用 [XAsyncSchedule](https://docs.xbox.com/zh-CN/reference/system/xasyncprovider/functions/xasyncschedule.md) 或通过外部方式开始其异步工作。`XAsyncBegin` 返回后，假定回调函数已在系统定义的线程上开始异步工作。

> [!NOTE]
>
> *provider* 中指定的回调函数在 `XAsyncBegin` 调用链中同步处理。回调函数执行的异步工作不得阻塞线程。

可以使用 *identity* 和 *identityName* 参数来标识和管理对回调函数不匹配的 `XAsyncOp::Begin` 和 `XAsyncOp::GetResult` 调用。通常，*identity* 的值设置为调用 `XAsyncBegin` 的函数的函数指针，*identityName* 的值设置为 `__FUNCTION__` 编译器宏。

## Requirements

**头文件：** XAsyncProvider.h

**库：** xgameruntime.lib

**受支持的平台：** Windows、XBOX One 系列主机和 XBOX Series 主机

## Conceptual documentation

* [设置自定义提供程序](https://docs.xbox.com/zh-CN/build/core-features/common/async/async-libraries/async-library-xasyncprovider-example-setup-custom-provider.md)
* [XAsyncProvider 库概述](https://docs.xbox.com/zh-CN/build/core-features/common/async/async-libraries/async-library-xasyncprovider.md)
* [异步编程模型](https://docs.xbox.com/zh-CN/build/core-features/common/async/async-programming-model.md)

## See also

[XAsyncProvider Members](https://docs.xbox.com/zh-CN/reference/system/xasyncprovider/xasyncprovider_members.md)\
[XAsyncProviderData](https://docs.xbox.com/zh-CN/reference/system/xasyncprovider/structs/xasyncproviderdata.md)\
[XAsyncProvider](https://docs.xbox.com/zh-CN/reference/system/xasyncprovider/functions/xasyncprovider.md)\
[XAsyncGetResult](https://docs.xbox.com/zh-CN/reference/system/xasyncprovider/functions/xasyncgetresult.md)\
[异步编程模型](https://docs.xbox.com/zh-CN/build/core-features/common/async/async-programming-model.md)


## Related topics

- [XAsyncComplete](https://docs.xbox.com/zh-CN/reference/system/xasyncprovider/functions/xasynccomplete.md)
- [XAsyncSchedule](https://docs.xbox.com/zh-CN/reference/system/xasyncprovider/functions/xasyncschedule.md)
- [XAsyncGetResult](https://docs.xbox.com/zh-CN/reference/system/xasyncprovider/functions/xasyncgetresult.md)
- [XAsyncProviderData](https://docs.xbox.com/zh-CN/reference/system/xasyncprovider/structs/xasyncproviderdata.md)
- [XAsyncProvider 库概述](https://docs.xbox.com/zh-CN/build/core-features/common/async/async-libraries/async-library-xasyncprovider.md)
