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

# 设置可取消性示例

> 设置可取消性示例

本主题提供了一个如何在自定义提供程序回调中设置可取消性的示例。使用 [XAsyncRun](/reference/system/xasync/functions/xasyncrun) 启动的基本异步方法无法取消。当你使用自定义提供程序时，可以手动添加可取消性支持。添加支持后，[XAsyncCancel](/reference/system/xasync/functions/xasynccancel) 会触发 `Cancel` 分支。

```c++ theme={null}
// Provider callback.
[](XAsyncOp op, const XAsyncProviderData* providerData)
{
    switch(op)
    {
        case XAsyncOp::DoWork:
        {
            bool canceled = false;
            while (true)
            {
                DWORD waitResult = WaitForSingleObject(callData->cancelEvent, 0);
                if (waitResult != WAIT_TIMEOUT)
                {
                    canceled = true;
                    break;
                }

                // Continue doing normal work. Should break if completed.
            }

            if (canceled)
            {
                XAsyncComplete(providerData->async, E_ABORT, 0);
            }
            else
            {
                XAsyncComplete(providerData->async, S_OK, 0);
            }

            break;
        }
        
        case XAsyncOp::Cancel:
            SetEvent(callData->cancelEvent);
            break;

        // Other cases.
    }
}
```

在这些提供程序分支中，通过使用 Windows 事件来添加可取消性支持。如果调用了 `Cancel` 分支，就会设置该事件；而 `DoWork` 分支会以调用 [XAsyncComplete](/reference/system/xasyncprovider/functions/xasynccomplete) 并使用 `E_ABORT` 状态结束。可以使用任何方法来发送取消信号。不过，事件是一种可在任何 Microsoft Game Development Kit (GDK) 目标平台上支持的简单取消信号方式。

要触发 `Cancel` 分支，请使用任务的异步块调用 `XAsyncCancel`，如下所示。

```c++ theme={null}
XAsyncCancel(async);
```

## 参考 API 文档

* [XAsyncProvider (API 内容)](/reference/system/xasyncprovider/xasyncprovider_members)
  * 函数
    * [XAsyncComplete](/reference/system/xasyncprovider/functions/xasynccomplete)
* [xasync (API 内容)](/reference/system/xasync/xasync_members)
  * 函数
    * [XAsyncRun](/reference/system/xasync/functions/xasyncrun)
    * [XAsyncCancel](/reference/system/xasync/functions/xasynccancel)

## 另请参阅

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

[设置自定义提供程序（示例）](/build/core-features/common/async/async-libraries/async-library-xasyncprovider-example-setup-custom-provider)

[设置调用方法（示例）](/build/core-features/common/async/async-libraries/async-library-xasyncprovider-example-setup-invocation-methods)

[设置返回数据（示例）](/build/core-features/common/async/async-libraries/async-library-xasyncprovider-example-setup-return-data)

[XAsyncProvider](/reference/system/xasyncprovider/xasyncprovider_members)


## Related topics

- [用于异步任务编程的 XAsync 库](/zh-CN/build/core-features/common/async/async-libraries/index.md)
- [设置调用方法示例](/zh-CN/build/core-features/common/async/async-libraries/async-library-xasyncprovider-example-setup-invocation-methods.md)
- [设置返回数据示例](/zh-CN/build/core-features/common/async/async-libraries/async-library-xasyncprovider-example-setup-return-data.md)
- [XAsyncProvider 库概述](/zh-CN/build/core-features/common/async/async-libraries/async-library-xasyncprovider.md)
- [XAsyncCancel](/zh-CN/reference/system/xasync/functions/xasynccancel.md)
