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

# Duplicating a task queue handle

> Example that calls XTaskQueueDuplicateHandle so long-running work keeps a valid task queue handle even after another caller runs XTaskQueueCloseHandle.

## 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](https://docs.xbox.com/build/core-features/common/async/async-task-queue-design.md)


## Related topics

- [Async task queue design](https://docs.xbox.com/build/core-features/common/async/async-task-queue-design.md)
- [XTaskQueueDuplicateHandle](https://docs.xbox.com/reference/system/xtaskqueue/functions/xtaskqueueduplicatehandle.md)
- [XTaskQueueCloseHandle](https://docs.xbox.com/reference/system/xtaskqueue/functions/xtaskqueueclosehandle.md)
- [XTaskQueue library overview](https://docs.xbox.com/build/core-features/common/async/async-libraries/async-library-xtaskqueue.md)
- [Making async calls in the PlayFab Unified SDK](https://docs.xbox.com/services/playfab/sdks/unified-sdk/async-model.md)
