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

# How to: Duplicating a task queue handle

> How to: Duplicating a task queue handle

## Example

If you have long-running work, you might want to duplicate the task queue handle for the duration of the work. In this way, anyone calling `XTaskQueueCloseHandle` won't close the queue while you still need it. This is shown in the following example.

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

## See also

[Designing the task queue](/build/core-features/common/async/async-task-queue-design)


## Related topics

- [Async task queue design](/build/core-features/common/async/async-task-queue-design.md)
- [How to: Using a task queue waiter](/build/core-features/common/async/async-task-queue-design-howto/using-task-queue-waiter.md)
- [How to: Using the process task queue](/build/core-features/common/async/async-task-queue-design-howto/using-process-task-queue.md)
- [XTaskQueueDuplicateHandle](/reference/system/xtaskqueue/functions/xtaskqueueduplicatehandle.md)
- [How to: Creating a composite task queue](/build/core-features/common/async/async-task-queue-design-howto/creating-composite-task-queue.md)
