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

# Set up cancelability example

> Set up cancelability example

This topic provides an example of how to set up cancelability in a custom provider callback. Basic async methods that are started with [XAsyncRun](/reference/system/xasync/functions/xasyncrun) can't be
canceled. Cancelability support can be manually added when you're using a
custom provider. After support is added, [XAsyncCancel](/reference/system/xasync/functions/xasynccancel)
triggers the `Cancel` case.

```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.
    }
}
```

In these provider cases, cancelability support is added by using a
Windows event. It's set if the `Cancel` case is called, and the
`DoWork` case ends with a call to [XAsyncComplete](/reference/system/xasyncprovider/functions/xasynccomplete) with
the `E_ABORT` status. Any method can be used to signal the cancel. However,
an event is a simple way to signal the cancel that's supported on any Microsoft Game Development Kit (GDK) platform that can be targeted.

To trigger the `Cancel` case, call `XAsyncCancel`
with the async block for the task, shown as follows.

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

## Reference API documentation

* [XAsyncProvider (API contents)](/reference/system/xasyncprovider/xasyncprovider_members)
  * Functions
    * [XAsyncComplete](/reference/system/xasyncprovider/functions/xasynccomplete)
* [xasync (API contents)](/reference/system/xasync/xasync_members)
  * Functions
    * [XAsyncRun](/reference/system/xasync/functions/xasyncrun)
    * [XAsyncCancel](/reference/system/xasync/functions/xasynccancel)

## See also

[XAsyncProvider library overview](/build/core-features/common/async/async-libraries/async-library-xasyncprovider)

[Set up custom provider (example)](/build/core-features/common/async/async-libraries/async-library-xasyncprovider-example-setup-custom-provider)

[Set up invocation methods (example)](/build/core-features/common/async/async-libraries/async-library-xasyncprovider-example-setup-invocation-methods)

[Set up return data (example)](/build/core-features/common/async/async-libraries/async-library-xasyncprovider-example-setup-return-data)

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


## Related topics

- [Set up invocation methods example](/build/core-features/common/async/async-libraries/async-library-xasyncprovider-example-setup-invocation-methods.md)
- [Set up return data example](/build/core-features/common/async/async-libraries/async-library-xasyncprovider-example-setup-return-data.md)
- [XAsyncProvider library overview](/build/core-features/common/async/async-libraries/async-library-xasyncprovider.md)
- [XAsync libraries for asynchronous task programming](/build/core-features/common/async/async-libraries/index.md)
- [Set up async task example](/build/core-features/common/async/async-libraries/async-library-xasync-example-setup-async-task.md)
