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

# XTaskQueueSubmitDelayedCallback

> XTaskQueueSubmitDelayedCallback

# XTaskQueueSubmitDelayedCallback

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

## Syntax

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

### Parameters

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

要向其提交回调的队列。

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

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

*delayMs*   \_In\_\
类型：uint32\_t

在将回调提交到队列之前要延迟的毫秒数。

<Note>可以提交回调，让其在将来某个时间添加到队列。如果 *delayMs* 为非零值，则回调将放置在待处理列表中，并使用下一个最接近的等待时间来初始化计时器。经过该时间后，具有下一个时间的回调将放置在执行列表中。队列将引发 [XTaskQueueMonitorCallback](/reference/system/xtaskqueue/functions/xtaskqueuemonitorcallback)，如果队列以线程池调度模式运行，则会提交线程池工作。</Note>

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

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

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

指向回调函数的指针。

### Return value

类型：HRESULT

HRESULT 成功或错误代码。

## Remarks

此回调将在 *delayMs* 毫秒后添加到队列。

<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

以下示例允许通过使用 **XTaskQueueSubmitDelayedCallback** 函数在将来提交回调。这是短暂延迟后重试失败调用的绝佳方式；它也可以用作定期事件的低成本计时器。

<Note>**SubmitCallback** 是在 [XTaskQueueSubmitCallback](/reference/system/xtaskqueue/functions/xtaskqueuesubmitcallback) 函数的代码示例中定义的帮助程序函数。</Note>

```cpp theme={null}
void SubmittingDelayedCallback()
{
    XTaskQueueHandle queue;

    HRESULT hr = XTaskQueueCreate(
        XTaskQueueDispatchMode::ThreadPool,
        XTaskQueueDispatchMode::ThreadPool, 
        &queue);

    if (FAILED(hr))
    {
        printf("failed to create task queue: 0x%x\r\n", hr);
        return;
    }

    struct CallContext
    {
        DWORD count;
        XTaskQueueHandle queue;
        XTaskQueueCallback* callback;
    } callContext;

    auto callback = [](void* context, bool cancel)
    {
        CallContext *callContext = static_cast<CallContext*>(context);
        callContext->count++;
        printf("Periodic callback %d\r\n", callContext->count);
        if (callContext->count != 10 && !cancel)
        {
            HRESULT hr = XTaskQueueSubmitDelayedCallback(
                callContext->queue, 
                XTaskQueuePort::Completion, 
                500, 
                callContext, 
                callContext->callback);

            if (FAILED(hr))
            {
                printf("Failed submitting next callback: 0x%x\r\n", hr);
                callContext->count = 10; // Prevent us from waiting forever.
            }
        }
    };

    callContext.count = 0;
    callContext.queue = queue;
    callContext.callback = callback;

    // Use the task queue to make 10 periodic calls
    hr = XTaskQueueSubmitDelayedCallback(
        queue,
        XTaskQueuePort::Completion,
        500,
        &callContext,
        callback);

    if (FAILED(hr))
    {
        printf("Failed submitting delayed callback: 0x%x\r\n", hr);
        XTaskQueueCloseHandle(queue);
        return;
    }

    // Now wait for all the calls to complete.
    while (callContext.count != 10)
    {
        Sleep(1000);
    }

    XTaskQueueTerminate(queue, true, nullptr, nullptr);
    XTaskQueueCloseHandle(queue);
}
```

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

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