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

將回呼提交至佇列的指定連接埠。

## 語法

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

### 參數

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

要提交回呼的佇列。

*port*   \_In\_\
類型：[XTaskQueuePort](/zh-TW/reference/system/xtaskqueue/enums/xtaskqueueport)

要提交回呼的連接埠。回呼可以指派給工作連接埠或完成連接埠。

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

將回呼提交至佇列之前要延遲的毫秒數。

<Note>回呼可以提交為在未來某個時間加入佇列。如果 *delayMs* 不是零，回呼會放在擱置清單中，並以下一個最接近的等候時間初始化計時器。當時間經過後，具有下一個時間的回呼會放在執行清單中。佇列會引發 [XTaskQueueMonitorCallback](/zh-TW/reference/system/xtaskqueue/functions/xtaskqueuemonitorcallback)，而如果佇列以執行緒集區分派模式執行，則會提交執行緒集區工作。</Note>

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

將傳遞給回呼的選擇性內容指標。

*callback*   \_In\_\
類型：[XTaskQueueCallback\*](/zh-TW/reference/system/xtaskqueue/functions/xtaskqueuecallback)

回呼函式的指標。

### 傳回值

類型：HRESULT

HRESULT 成功或錯誤碼。

## 備註

此回呼會在 *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](/zh-TW/reference/system/xtaskqueue/functions/xtaskqueuesubmitcallback)
* [XTaskQueueSubmitDelayedCallback](/zh-TW/reference/system/xtaskqueue/functions/xtaskqueuesubmitdelayedcallback)
* [XTaskQueueDispatch](/zh-TW/reference/system/xtaskqueue/functions/xtaskqueuedispatch)，前提是 *timeoutInMs* 傳遞 0

下列範例使用 **XTaskQueueSubmitDelayedCallback** 函式，讓回呼可以在未來提交。這是在短暫延遲後重試失敗呼叫的絕佳方式；也可以用作定期事件的低成本計時器。

<Note>**SubmitCallback** 是在 [XTaskQueueSubmitCallback](/zh-TW/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);
}
```

## 需求

**標頭：** XTaskQueue.h

**程式庫：** xgameruntime.lib

**支援的平台：** Windows、XBOX One 系列主機和 XBOX Series 主機

## 另請參閱

[XTaskQueue 成員](/zh-TW/reference/system/xtaskqueue/xtaskqueue_members)\
[非同步程式設計模型](/zh-TW/build/core-features/common/async/async-programming-model)\
[非同步工作佇列設計](/zh-TW/build/core-features/common/async/async-task-queue-design)


## Related topics

- [XTaskQueueSubmitDelayedCallback](/reference/system/xtaskqueue/functions/xtaskqueuesubmitdelayedcallback.md)
- [How to: Using delayed callbacks](/build/core-features/common/async/async-task-queue-design-howto/using-delayed-callbacks.md)
- [XTaskQueueSubmitCallback](/reference/system/xtaskqueue/functions/xtaskqueuesubmitcallback.md)
- [XTaskQueuePort](/reference/system/xtaskqueue/enums/xtaskqueueport.md)
- [XTaskQueueMonitorCallback](/reference/system/xtaskqueue/functions/xtaskqueuemonitorcallback.md)
