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

# 如何：复制任务队列句柄

> 如何：复制任务队列句柄

## 示例

如果你有长时间运行的工作，可能希望在工作持续期间复制任务队列句柄。这样，任何调用 `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); 
    } 
} 
```

## 另请参阅

[设计任务队列](/build/core-features/common/async/async-task-queue-design)


## Related topics

- [XTaskQueueDuplicateHandle](/zh-CN/reference/system/xtaskqueue/functions/xtaskqueueduplicatehandle.md)
- [异步任务队列设计](/zh-CN/build/core-features/common/async/async-task-queue-design.md)
- [如何：使用进程任务队列](/zh-CN/build/core-features/common/async/async-task-queue-design-howto/using-process-task-queue.md)
- [XTaskQueueSetCurrentProcessTaskQueue](/zh-CN/reference/system/xtaskqueue/functions/xtaskqueuesetcurrentprocesstaskqueue.md)
- [如何使用 XBOX 服务 API (XSAPI)](/zh-CN/services/xbox-services/fundamentals/xbox-services-api/live-gs-xbl-apis.md)
