> ## 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` API，在未來的時間點提交回呼。如此一來，您可以在短暫延遲後重試失敗的呼叫，或將其當作定期事件的低成本計時器使用。

下列範例每 500 毫秒叫用一次回呼，共叫用 10 次。

```c++ 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; // Prevents 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 of the calls to be completed.  
    while (callContext.count != 10)  
    {  
        Sleep(1000);  
    }  
  
    XTaskQueueTerminate(queue, true, nullptr, nullptr);  
    XTaskQueueCloseHandle(queue);  
}  
```

## 範例輸出

```
Periodic callback 1  
Periodic callback 2  
Periodic callback 3  
Periodic callback 4  
Periodic callback 5  
Periodic callback 6  
Periodic callback 7  
Periodic callback 8  
Periodic callback 9  
Periodic callback 10  
```

## 另請參閱

[設計工作佇列](/zh-TW/build/core-features/common/async/async-task-queue-design)


## Related topics

- [如何：使用延迟回调](/zh-CN/build/core-features/common/async/async-task-queue-design-howto/using-delayed-callbacks.md)
- [XBOX 合作夥伴加入流程:取得存取權限與發行](/zh-TW/home/onboarding.md)
- [Microsoft Store v8 Collections b2bLicensePreview API](/zh-CN/reference/microsoft-store-apis/xstore-v8-query-for-products.md)
- [collections.mp.microsoft.com/v9.0/collections/publisherQuery](/zh-CN/reference/microsoft-store-apis/xstore-v9-query-for-products.md)
- [IXtfCopyFileCallback::OnStartFileCopy Method](/ja-jp/reference/tools/xtf/xtffileio/classes/IXtfCopyFileCallback/methods/onstartfilecopy-ixtfcopyfilecallback-xtffileio-xbox-microsoft-m.md)
