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

# XTaskQueueSubmitCallback

> XTaskQueueSubmitCallback

# XTaskQueueSubmitCallback

将回调提交到给定端口的队列。

## Syntax

```cpp theme={null}
HRESULT XTaskQueueSubmitCallback(  
         XTaskQueueHandle queue,  
         XTaskQueuePort port,  
         void* callbackContext,  
         XTaskQueueCallback* callback  
)  
```

### Parameters

*queue*   \_In\_\
类型：XTaskQueueHandle

要向其提交回调的队列。

*port*   \_In\_\
类型：[XTaskQueuePort](/reference/system/xtaskqueue/enums/xtaskqueueport)

要向其提交回调的端口。回调可以分配给工作端口或完成端口。

*callbackContext*   \_In\_opt\_\
类型：void\*

将传递给回调的可选上下文指针。

*callback*   \_In\_\
类型：[XTaskQueueCallback\*](/reference/system/xtaskqueue/functions/xtaskqueuecallback)

指向回调函数的指针。

### Return value

类型：HRESULT

HRESULT 成功或错误代码。

## Remarks

<img src="https://mintcdn.com/microsoft-4404708b/gzrdvT5kpqAXKEQt/images/gdk/reference/note.gif?s=6c1a4c009b25ad5315c45ce51bc83983" alt="alert" width="10" height="10" data-path="images/gdk/reference/note.gif" /> **性能说明：**\
向队列添加项目必须是无锁且无等待的，以满足上游代码的性能要求。以下 API 是无锁的：

* [XTaskQueueSubmitCallback](/reference/system/xtaskqueue/functions/xtaskqueuesubmitcallback)
* [XTaskQueueSubmitDelayedCallback](/reference/system/xtaskqueue/functions/xtaskqueuesubmitdelayedcallback)
* [XTaskQueueDispatch](/reference/system/xtaskqueue/functions/xtaskqueuedispatch)，前提是为 *timeoutInMs* 传递 0

以下示例演示了如何将回调提交到任务队列的工作端口或完成端口。它先提交一个工作回调，然后在工作回调结束时提交一个完成回调，这是很典型的方式。

```cpp 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);
    }
}
```

## Requirements

**头文件：** XTaskQueue.h

**库：** xgameruntime.lib

**受支持的平台：** Windows、XBOX One 系列主机和 XBOX Series 主机

## See also

[XTaskQueue 成员](/reference/system/xtaskqueue/xtaskqueue_members)\
[异步编程模型](/build/core-features/common/async/async-programming-model)\
[异步任务队列设计](/build/core-features/common/async/async-task-queue-design)


## Related topics

- [XTaskQueueSubmitDelayedCallback](/zh-CN/reference/system/xtaskqueue/functions/xtaskqueuesubmitdelayedcallback.md)
- [XTaskQueuePort](/zh-CN/reference/system/xtaskqueue/enums/xtaskqueueport.md)
- [XTaskQueueCallback](/zh-CN/reference/system/xtaskqueue/functions/xtaskqueuecallback.md)
- [XTaskQueueMonitorCallback](/zh-CN/reference/system/xtaskqueue/functions/xtaskqueuemonitorcallback.md)
- [XTaskQueueDispatch](/zh-CN/reference/system/xtaskqueue/functions/xtaskqueuedispatch.md)
