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

# Submitting callbacks

> How to: Submitting callbacks

## Example

The following example shows how to submit a callback to either the work port
or the completion port of a task queue. First, it submits a work callback. Then, at the end of the work callback, it submits a completion callback. This is typical usage. The `SubmitCallbacks` function is
used in subsequent scenarios for brevity.

```c++ theme={null}
void CALLBACK SampleCompletionCallback(void*, bool cancel)
{
    printf("Completion invoked on thread %d. Cancel? %d.\r\n",
    GetCurrentThreadId(), cancel);
}

void CALLBACK SampleWorkCallback(void* context, bool cancel)
{
    printf("Worker invoked on thread %d. Cancel? %d.\r\n",
    GetCurrentThreadId(), cancel);

    XTaskQueueHandle queueFromContext = static_cast<XTaskQueueHandle>(context);

    HRESULT hrCompletion = XTaskQueueSubmitCallback(
        queueFromContext,
        XTaskQueuePort::Completion,
        nullptr,
        SampleCompletionCallback);

    if (FAILED(hrCompletion))
    {
        printf("Error 0x%x submitting completion.\r\n", hrCompletion);
    }
}

void SubmitCallbacks(XTaskQueueHandle queue)
{
    HRESULT hrWork = XTaskQueueSubmitCallback(
        queue,
        XTaskQueuePort::Work,
        queue,
        SampleWorkCallback);

    if (FAILED(hrWork))
    {
        printf("Error 0x%x submitting work.\r\n", hrWork);
    }
}
```

## See also

[Designing the task queue](https://docs.xbox.com/build/core-features/common/async/async-task-queue-design.md)


## Related topics

- [XTaskQueueCallback](https://docs.xbox.com/reference/system/xtaskqueue/functions/xtaskqueuecallback.md)
- [XTaskQueuePort](https://docs.xbox.com/reference/system/xtaskqueue/enums/xtaskqueueport.md)
- [Async task queue design](https://docs.xbox.com/build/core-features/common/async/async-task-queue-design.md)
- [XTaskQueueSubmitDelayedCallback](https://docs.xbox.com/reference/system/xtaskqueue/functions/xtaskqueuesubmitdelayedcallback.md)
- [XTaskQueueTerminate](https://docs.xbox.com/reference/system/xtaskqueue/functions/xtaskqueueterminate.md)
