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

# XTaskQueueRegisterMonitor

> XTaskQueueRegisterMonitor

# XTaskQueueRegisterMonitor

註冊一個回呼，每當有回呼提交至此佇列時就會叫用該回呼。

## 語法

```cpp theme={null}
HRESULT XTaskQueueRegisterMonitor(  
         XTaskQueueHandle queue,  
         void* callbackContext,  
         XTaskQueueMonitorCallback* callback,  
         XTaskQueueRegistrationToken* token  
)  
```

### 參數

*queue*   \_In\_\
類型：XTaskQueueHandle

要註冊提交回呼的佇列。

*callbackContext*   \_In\_opt\_\
類型：void\*

要傳遞給提交回呼的選擇性內容指標。

*callback*   \_In\_\
類型：[XTaskQueueMonitorCallback\*](/zh-TW/reference/system/xtaskqueue/functions/xtaskqueuemonitorcallback)

當有新的回呼提交至佇列時將叫用的回呼。

*token*   \_Out\_\
類型：[XTaskQueueRegistrationToken\*](/zh-TW/reference/system/xtaskqueue/structs/xtaskqueueregistrationtoken)

呼叫 [XTaskQueueUnregisterMonitor](/zh-TW/reference/system/xtaskqueue/functions/xtaskqueueunregistermonitor) 以移除回呼時使用的權杖。

### 傳回值

類型：HRESULT

HRESULT 成功或錯誤碼。

## 備註

<Note>在時間敏感執行緒上呼叫此函式並不安全。如需詳細資訊，請參閱[時間敏感執行緒](https://learn.microsoft.com/gaming/gdk/docs/gdk-dev/console-dev/overviews/threads/time-sensitive-threads)。</Note>

下列範例示範工作佇列在有項目要分派時，如何用來發出條件變數的訊號。應用程式可以利用此方式，將用於工作佇列的執行緒與應用程式中的其他工作共用。

<Note>**SubmitCallback** 是在 [XTaskQueueSubmitCallback](/zh-TW/reference/system/xtaskqueue/functions/xtaskqueuesubmitcallback) 函式的程式碼範例中定義的協助程式函式。</Note>

```cpp theme={null}
// We will use condition variables to signal when there is something for us 
// in the queue and a bool to signal when we want the whole thing to close.
// Keep all this in a struct for convenient access
struct QueueControl
{
    std::condition_variable workActivity;
    std::mutex workMutex;
    std::condition_variable completionActivity;
    std::mutex completionMutex;
    bool terminate = false;
} queueControl;

void CALLBACK TaskQueueNewItemSubmitted(void* context, XTaskQueueHandle, XTaskQueuePort port)
{
    // A new callback has been submitted. notify the correct condition variable
    QueueControl* queueControl = static_cast<QueueControl*>(context);
    switch (port)
    {
    case XTaskQueuePort::Work:
        queueControl->workActivity.notify_all();
        break;

    case XTaskQueuePort::Completion:
        queueControl->completionActivity.notify_all();
        break;
    }
}

void CreatingTaskQueueWithManualSignaling()
{
    // 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;
    }

    // Listen to callback submitted notifications to signal
    // our condition variable.
    XTaskQueueRegistrationToken token;
    hr = XTaskQueueRegisterMonitor(
        queue, &queueControl, 
        TaskQueueNewItemSubmitted, &token);

    std::thread workThread([&]()
    {
        std::unique_lock<std::mutex> lock(queueControl.workMutex);
        while (!queueControl.terminate)
        {
            queueControl.workActivity.wait(lock);
            XTaskQueueDispatch(queue, XTaskQueuePort::Work, 0);
        }
    });

    std::thread completionThread([&]()
    {
        std::unique_lock<std::mutex> lock(queueControl.completionMutex);
        while (!queueControl.terminate)
        {
            queueControl.completionActivity.wait(lock);
            XTaskQueueDispatch(queue, XTaskQueuePort::Completion, 0);
        }
    });

    SubmitCallbacks(queue);

    // Wait a while for the callbacks to run
    Sleep(1000);

    XTaskQueueTerminate(queue, true, nullptr, nullptr);

    queueControl.terminate = true;
    queueControl.workActivity.notify_all();
    queueControl.completionActivity.notify_all();

    workThread.join();
    completionThread.join();
}
```

## 需求

**標頭：** XTaskQueue.h

**程式庫：** xgameruntime.lib

**支援的平台：** Windows、XBOX One 系列主機和 XBOX Series 主機

## 概念文件

* [XTaskQueue 程式庫概觀](/zh-TW/build/core-features/common/async/async-libraries/async-library-xtaskqueue)
* [時間敏感執行緒](https://learn.microsoft.com/gaming/gdk/docs/gdk-dev/console-dev/overviews/threads/time-sensitive-threads)

## 另請參閱

[XTaskQueue 成員](/zh-TW/reference/system/xtaskqueue/xtaskqueue_members)\
[非同步程式設計模型](/zh-TW/build/core-features/common/async/async-programming-model)\
[非同步工作佇列設計](/zh-TW/build/core-features/common/async/async-task-queue-design)


## Related topics

- [XTaskQueueRegisterMonitor](/reference/system/xtaskqueue/functions/xtaskqueueregistermonitor.md)
- [XTaskQueueUnregisterMonitor](/reference/system/xtaskqueue/functions/xtaskqueueunregistermonitor.md)
- [XTaskQueueMonitorCallback](/reference/system/xtaskqueue/functions/xtaskqueuemonitorcallback.md)
