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

# Como: usar retornos de chamada atrasados

> Como: usar retornos de chamada atrasados

## Exemplo

Use uma fila de tarefas para enviar retornos de chamada no futuro usando a API `XTaskQueueSubmitDelayedCallback`. Dessa forma, você pode repetir uma chamada com falha após um pequeno atraso ou usá-la como um temporizador barato para eventos periódicos.

O exemplo a seguir invoca 10 retornos de chamada, um a cada 500 ms.

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

## Exemplo de saída

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

## Consulte também

[Projetando a fila de tarefas](/pt-BR/build/core-features/common/async/async-task-queue-design)


## Related topics

- [PFLeaderboardsListLeaderboardDefinitionsGetResult](/pt-BR/services/playfab/api-references/c/pfleaderboards/functions/pfleaderboardslistleaderboarddefinitionsgetresult.md)
- [PFLeaderboardsListLeaderboardDefinitionsGetResultSize](/pt-BR/services/playfab/api-references/c/pfleaderboards/functions/pfleaderboardslistleaderboarddefinitionsgetresultsize.md)
- [PFLocalizationGetLanguageListGetResultSize](/pt-BR/services/playfab/api-references/c/pflocalization/functions/pflocalizationgetlanguagelistgetresultsize.md)
- [PFLocalizationGetLanguageListGetResult](/pt-BR/services/playfab/api-references/c/pflocalization/functions/pflocalizationgetlanguagelistgetresult.md)
- [PFStatisticsGetStatisticDefinitionGetResult](/pt-BR/services/playfab/api-references/c/pfstatistics/functions/pfstatisticsgetstatisticdefinitiongetresult.md)
