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

## 另請參閱

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


## Related topics

- [PlayFab Multiplayer C++ SDK 版本資訊](/zh-TW/services/playfab/multiplayer/lobby/lobby-matchmaking-sdks/lobby-and-matchmaking-release-notes.md)
- [GDK 範例](/zh-TW/home/setup-install/samples.md)
- [PlayFab Party 版本資訊](/zh-TW/services/playfab/multiplayer/networking/release-notes.md)
- [游戏无障碍工作坊工具包](/zh-CN/build/game-principles/accessibility/accessibility-workshop-toolkit.md)
- [管理并授权可下载内容 (DLC)](/zh-CN/publishing/xstore-commerce/xstore-dlc.md)
