> ## 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](/zh-TW/reference/system/xasync/functions/xasyncrun) 啟動的基本非同步方法無法
取消。使用自訂提供者時，可以手動新增可取消性支援。新增支援之後，[XAsyncCancel](/zh-TW/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` 案例會以 `E_ABORT` 狀態呼叫 [XAsyncComplete](/zh-TW/reference/system/xasyncprovider/functions/xasynccomplete)
作為結束。任何方法都可用來發出取消訊號。不過，
事件是一種發出取消訊號的簡單方式，且在任何可作為目標的 Microsoft Game Development Kit (GDK) 平台上都受到支援。

若要觸發 `Cancel` 案例，請使用工作的非同步區塊呼叫 `XAsyncCancel`，
如下所示。

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

## 參考 API 文件

* [XAsyncProvider (API 內容)](/zh-TW/reference/system/xasyncprovider/xasyncprovider_members)
  * 函式
    * [XAsyncComplete](/zh-TW/reference/system/xasyncprovider/functions/xasynccomplete)
* [xasync (API 內容)](/zh-TW/reference/system/xasync/xasync_members)
  * 函式
    * [XAsyncRun](/zh-TW/reference/system/xasync/functions/xasyncrun)
    * [XAsyncCancel](/zh-TW/reference/system/xasync/functions/xasynccancel)

## 另請參閱

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

[設定自訂提供者 (範例)](/zh-TW/build/core-features/common/async/async-libraries/async-library-xasyncprovider-example-setup-custom-provider)

[設定叫用方法 (範例)](/zh-TW/build/core-features/common/async/async-libraries/async-library-xasyncprovider-example-setup-invocation-methods)

[設定傳回資料 (範例)](/zh-TW/build/core-features/common/async/async-libraries/async-library-xasyncprovider-example-setup-return-data)

[XAsyncProvider](/zh-TW/reference/system/xasyncprovider/xasyncprovider_members)


## Related topics

- [设置可取消性示例](/zh-CN/build/core-features/common/async/async-libraries/async-library-xasyncprovider-example-setup-cancelability.md)
- [XAsyncCancel](/zh-CN/reference/system/xasync/functions/xasynccancel.md)
- [XAsyncComplete](/zh-CN/reference/system/xasyncprovider/functions/xasynccomplete.md)
- [设置返回数据示例](/zh-CN/build/core-features/common/async/async-libraries/async-library-xasyncprovider-example-setup-return-data.md)
- [设置调用方法示例](/zh-CN/build/core-features/common/async/async-libraries/async-library-xasyncprovider-example-setup-invocation-methods.md)
