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

# 操作說明：複製工作佇列控制代碼

> 此範例呼叫 XTaskQueueDuplicateHandle，讓長時間執行的工作即使在其他呼叫端執行 XTaskQueueCloseHandle 後，仍保有有效的工作佇列控制代碼。

## 範例

如果您有長時間執行的工作，可能會想要在工作期間複製工作佇列控制代碼。如此一來，在您仍需要佇列時，任何呼叫 `XTaskQueueCloseHandle` 的人都不會關閉該佇列。下列範例說明了這一點。

```c++ theme={null}
void DuplicatingTaskQueueHandle() 
{ 
    XTaskQueueHandle queue; 
 
    HRESULT hr = XTaskQueueCreate( 
        XTaskQueueDispatchMode::Manual,  
        XTaskQueueDispatchMode::Manual,  
        &queue); 
 
    if (FAILED(hr)) 
    { 
        printf("Failed to create task queue: 0x%x\r\n", hr); 
        return; 
    } 
 
    class LongRunningWork 
    { 
    public: 
        HRESULT Initialize(XTaskQueueHandle queue) 
        { 
            return XTaskQueueDuplicateHandle(queue, &m_queue); 
        } 
         
        ~LongRunningWork() 
        { 
            if (m_queue != nullptr) 
            { 
                XTaskQueueCloseHandle(m_queue); 
            } 
        } 
 
    private: 
        XTaskQueueHandle m_queue = nullptr; 
    }; 
 
    LongRunningWork work; 
    hr = work.Initialize(queue); 
     
    // Note that the queue handle for LongRunningWork is still valid. 
    XTaskQueueCloseHandle(queue); 
 
    if (FAILED(hr)) 
    { 
        printf("Failed to duplicate queue handle: 0x%x\r\n", hr); 
    } 
} 
```

## 另請參閱

[設計工作佇列](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/using-process-task-queue.md)
- [XTaskQueueDuplicateHandle](https://docs.xbox.com/zh-TW/reference/system/xtaskqueue/functions/xtaskqueueduplicatehandle.md)
- [操作說明：使用工作佇列等候程式](https://docs.xbox.com/zh-TW/build/core-features/common/async/async-task-queue-design-howto/using-task-queue-waiter.md)
- [非同步工作佇列設計](https://docs.xbox.com/zh-TW/build/core-features/common/async/async-task-queue-design.md)
- [操作說明：建立手動工作佇列](https://docs.xbox.com/zh-TW/build/core-features/common/async/async-task-queue-design-howto/creating-manual-task-queue.md)
