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

# Wrapper genérico de tarefa assíncrona

> Wrapper genérico de tarefa assíncrona

Este tópico fornece um exemplo de um wrapper genérico de tarefa assíncrona. Em vez de configurar manualmente blocos assíncronos e chamar [XAsyncRun](/pt-BR/reference/system/xasync/functions/xasyncrun)
para cada tarefa, crie um wrapper `RunTask` para simplificar esse
processo. Usando esse wrapper, você pode especificar uma fila de tarefas, um trabalho e um retorno de chamada de conclusão opcional.

```c++ theme={null}
void RunTask(XTaskQueueHandle taskQueue,
    std::function<void()> workCallback,
    std::function<void()> completionCallback)
{
    struct RunTaskContext
    {
        std::function<void()> workCallback;
        std::function<void()> completionCallback;
    };

    RunTaskContext* context = new RunTaskContext();
    context->workCallback = workCallback;
    context->completionCallback = completionCallback;
    XAsyncBlock* async = new XAsyncBlock{};
    async->queue = taskQueue;
    async->context = context;
    async->callback = [](XAsyncBlock* async)
    {
        RunTaskContext* context = static_cast<RunTaskContext*>(async->context);
        context->completionCallback();
        delete context;
        delete async;
    };

    // Callback passed to XAsyncRun is the work callback
    XAsyncRun(async,
        [](XAsyncBlock* async)->HRESULT
        {
            RunTaskContext* context = static_cast<RunTaskContext*>(async->context);
            context->workCallback();
            return S_OK;
        });
}
```

O exemplo anterior implementa uma função que recebe um bloco assíncrono e dois
retornos de chamada: um de trabalho e um de conclusão. Você pode escrever uma versão que
receba apenas um retorno de chamada de trabalho, pois o retorno de chamada de conclusão é opcional.

Ele usa o padrão comum para configurar um bloco assíncrono, configurar os dados de
contexto e chamar [XAsyncRun](/pt-BR/reference/system/xasync/functions/xasyncrun). `RunTask` encapsula o código
clichê, criando uma função de tarefa independente simples. A captura simples de dados é passada para os retornos de chamada sem parâmetros de contexto usando `std::function`.

```c++ theme={null}
RunTask(taskQueue,
    []()
    {
        // Work Callback
    },
    []()
    {
        // Completion Callback
    });
```

## Documentação de referência da API

* [Xasync (conteúdo da API)](/pt-BR/reference/system/xasync/xasync_members)
  * Funções
    * [XAsyncRun](/pt-BR/reference/system/xasync/functions/xasyncrun)
* [Xtaskqueue (conteúdo da API)](/pt-BR/reference/system/xtaskqueue/xtaskqueue_members)

## Consulte também

[Visão geral da programação assíncrona](/pt-BR/build/core-features/common/async/async-toc)
[XAsync](/pt-BR/reference/system/xasync/xasync_members)
[XTaskQueue](/pt-BR/reference/system/xtaskqueue/xtaskqueue_members)


## Related topics

- [Exemplos do GDK](/pt-BR/home/setup-install/samples.md)
- [Notas de versão do PlayFab Party](/pt-BR/services/playfab/multiplayer/networking/release-notes.md)
- [PFLeaderboardsGetLeaderboardAsync](/pt-BR/services/playfab/api-references/c/pfleaderboards/functions/pfleaderboardsgetleaderboardasync.md)
- [PFLeaderboardsIncrementLeaderboardVersionAsync](/pt-BR/services/playfab/api-references/c/pfleaderboards/functions/pfleaderboardsincrementleaderboardversionasync.md)
- [PFLocalizationGetLanguageListAsync](/pt-BR/services/playfab/api-references/c/pflocalization/functions/pflocalizationgetlanguagelistasync.md)
