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

# 設定可取消性範例

> 在 Cancel 案例中發出事件訊號，並以 E_ABORT 結束 DoWork，為自訂非同步提供者新增取消支援，然後使用 XAsyncCancel 觸發取消。

本主題提供如何在自訂提供者回呼中設定可取消性的範例。使用 [XAsyncRun](https://docs.xbox.com/zh-TW/reference/system/xasync/functions/xasyncrun.md) 啟動的基本非同步方法無法
取消。使用自訂提供者時，可以手動新增可取消性支援。新增支援之後，[XAsyncCancel](https://docs.xbox.com/zh-TW/reference/system/xasync/functions/xasynccancel.md)
會觸發 `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](https://docs.xbox.com/zh-TW/reference/system/xasyncprovider/functions/xasynccomplete.md)
作為結束。任何方法都可用來發出取消訊號。不過，
事件是一種發出取消訊號的簡單方式，且在任何可作為目標的 Microsoft Game Development Kit (GDK) 平台上都受到支援。

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

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

## 參考 API 文件

* [XAsyncProvider (API 內容)](https://docs.xbox.com/zh-TW/reference/system/xasyncprovider/xasyncprovider_members.md)
  * 函式
    * [XAsyncComplete](https://docs.xbox.com/zh-TW/reference/system/xasyncprovider/functions/xasynccomplete.md)
* [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)
    * [XAsyncCancel](https://docs.xbox.com/zh-TW/reference/system/xasync/functions/xasynccancel.md)

## 另請參閱

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

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

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

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

[XAsyncProvider](https://docs.xbox.com/zh-TW/reference/system/xasyncprovider/xasyncprovider_members.md)


## Related topics

- [用於非同步工作程式設計的 XAsync 程式庫](https://docs.xbox.com/zh-TW/build/core-features/common/async/async-libraries/index.md)
- [設定傳回資料範例](https://docs.xbox.com/zh-TW/build/core-features/common/async/async-libraries/async-library-xasyncprovider-example-setup-return-data.md)
- [設定叫用方法範例](https://docs.xbox.com/zh-TW/build/core-features/common/async/async-libraries/async-library-xasyncprovider-example-setup-invocation-methods.md)
- [設定自訂提供者範例](https://docs.xbox.com/zh-TW/build/core-features/common/async/async-libraries/async-library-xasyncprovider-example-setup-custom-provider.md)
- [XAsyncProvider 程式庫概觀](https://docs.xbox.com/zh-TW/build/core-features/common/async/async-libraries/async-library-xasyncprovider.md)
