> ## Documentation Index
> Fetch the complete documentation index at: https://docs.xbox.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 操作說明：建立手動工作佇列

> 此範例以 Manual 分派模式建立工作佇列，並執行兩個 STL 執行緒，分別為工作連接埠和完成連接埠呼叫 XTaskQueueDispatch。

## 範例

您可能會想要明確控制用於工作佇列之工作與完成的執行緒。在這些
情況下，請建立*手動工作佇列*，並自行分派呼叫。如果您使用手動工作佇列，
則必須確保它們也會提取 Windows 訊息佇列。

下列範例示範如何建立以手動方式提取的工作佇列。它會建立兩個 STL
執行緒，分別為工作連接埠和完成連接埠分派呼叫。

```c++ theme={null}
void CreatingTaskQueueWithManualThreads() 
{  
    // Create a manual task queue.  
    XTaskQueueHandle queue;  
  
    HRESULT hr = XTaskQueueCreate(  
        XTaskQueueDispatchMode::Manual,  
        XTaskQueueDispatchMode::Manual,  
        &queue);  
  
    if (FAILED(hr))  
    {  
        printf("Creating queue failed: 0x%x\r\n", hr);  
        return;  
    }  
  
    // We create threads to pump the queue: one thread for the work port  
    // and one thread for the completion port.  
    std::thread workThread([queue]  
    {  
        // XTaskQueueDispatch returns false when there's nothing to  
        // dispatch. Here, we wait forever for something new to come  
        // in. This will return false only if the queue is being  
        // terminated.  
        while (XTaskQueueDispatch(queue, XTaskQueuePort::Work, INFINITE));  
    });  
  
    std::thread completionThread([queue]  
    {  
        // XTaskQueueDispatch returns false when there's nothing to  
        // dispatch. Here, we wait forever for something new to come  
        // in. This will return false only if the queue is being  
        // terminated.  
        while (XTaskQueueDispatch(queue, XTaskQueuePort::Completion, INFINITE));  
    });  
  
    SubmitCallbacks(queue);  
  
    // Wait a while for the callbacks to run.  
    Sleep(1000);  
  
    // Terminating the queue will cause a waiting XTaskQueueDispatch to return  
    // false.  
    XTaskQueueTerminate(queue, true, nullptr, nullptr);  
  
    workThread.join();  
    completionThread.join();  
}  
```

## 另請參閱

[設計工作佇列](https://docs.xbox.com/zh-TW/build/core-features/common/async/async-task-queue-design.md)


## Related topics

- [操作說明：建立執行緒集區工作佇列](https://docs.xbox.com/zh-TW/build/core-features/common/async/async-task-queue-design-howto/creating-thread-pool-task-queue.md)
- [操作說明：使用處理序工作佇列](https://docs.xbox.com/zh-TW/build/core-features/common/async/async-task-queue-design-howto/using-process-task-queue.md)
- [操作說明：使用工作佇列等候程式](https://docs.xbox.com/zh-TW/build/core-features/common/async/async-task-queue-design-howto/using-task-queue-waiter.md)
- [操作說明：搭配 Windows 訊息迴圈使用工作佇列](https://docs.xbox.com/zh-TW/build/core-features/common/async/async-task-queue-design-howto/task-queue-windows-msg-loop.md)
- [操作說明：複製工作佇列控制代碼](https://docs.xbox.com/zh-TW/build/core-features/common/async/async-task-queue-design-howto/duplicating-task-queue-handle.md)
