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

# 如何：创建复合任务队列

> 如何：创建复合任务队列

## 示例

*复合任务队列*是由其他队列的部分组成的任务队列。当一个异步任务需要调用另一个异步任务，并且该任务的完成只是一个中间步骤而不应浪费完成线程上的周期时，复合任务队列非常有用。在这种情况下，可以创建一个复合队列。该复合队列的工作端口和完成端口都使用原始队列的工作端口。

以下示例使用线程池处理工作，但将完成端口回调集成到 Win32 WindowProc 回调函数中。此示例还演示了将任务队列与其他线程模型集成时如何正确终止任务队列。

```c++ theme={null}
void CreatingCompositeQueue()  
{  
    XTaskQueueHandle queue;  
  
    HRESULT hr = XTaskQueueCreate(  
        XTaskQueueDispatchMode::ThreadPool,  
        XTaskQueueDispatchMode::Manual,  
        &queue);  
  
    if (FAILED(hr))  
    {  
        printf("Failed to create task queue: 0x%x\r\n", hr);  
        return;  
    }  
  
    XTaskQueuePortHandle workPort;  
  
    // Create a composite queue that uses the work port from  
    // another queue for both the work and the completion ports.  
  
    hr = XTaskQueueGetPort(queue, XTaskQueuePort::Work, &workPort);  
    if (FAILED(hr))  
    {  
        printf("Failed to get work port 0x%x\r\n", hr);  
        XTaskQueueCloseHandle(queue);  
        return;  
    }  
  
    XTaskQueueHandle compositeQueue;  
    hr = XTaskQueueCreateComposite(workPort, workPort, &compositeQueue);  
    if (FAILED(hr))  
    {  
        printf("Failed to create composite queue 0x%x\r\n", hr);  
        XTaskQueueCloseHandle(queue);  
        return;  
    }  
  
    // Use the queue as needed.  
    SubmitCallbacks(compositeQueue);  
  
    // Wait a while for the callbacks to run.  
    Sleep(1000);  
  
    XTaskQueueCloseHandle(compositeQueue);  
    XTaskQueueCloseHandle(queue);  
}  
```

另一种技巧是创建一个本地队列，其工作和完成分派类型均为*立即*。立即分派模式根本不涉及任何异步活动——它在回调提交时立即分派它们，如下面的代码示例所示。

```c++ theme={null}
void CreatingImmediateQueue()  
{  
    XTaskQueueHandle queue;  
  
    HRESULT hr = XTaskQueueCreate(  
        XTaskQueueDispatchMode::Immediate,  
        XTaskQueueDispatchMode::Immediate,  
        &queue);  
  
    if (FAILED(hr))  
    {  
        printf("Failed to create task queue: 0x%x\r\n", hr);  
        return;  
    }  
  
    // Use the queue as needed.  
    SubmitCallbacks(queue);  
  
    // Wait a while for the callbacks to run.  
    Sleep(1000);  
  
    XTaskQueueCloseHandle(queue);  
}  
```

## 另请参阅

[设计任务队列](/build/core-features/common/async/async-task-queue-design)


## Related topics

- [如何：创建手动任务队列](/zh-CN/build/core-features/common/async/async-task-queue-design-howto/creating-manual-task-queue.md)
- [如何：创建线程池任务队列](/zh-CN/build/core-features/common/async/async-task-queue-design-howto/creating-thread-pool-task-queue.md)
- [XTaskQueueGetPort](/zh-CN/reference/system/xtaskqueue/functions/xtaskqueuegetport.md)
- [XTaskQueueCreateComposite](/zh-CN/reference/system/xtaskqueue/functions/xtaskqueuecreatecomposite.md)
- [异步任务队列设计](/zh-CN/build/core-features/common/async/async-task-queue-design.md)
