Share via


Registering for Policy Refresh Notification

The following code example shows how to be aware of a policy refresh by using a background thread to receive notifications.

//
// Call CreateThread to monitor for Group Policy notifications.
//

hThread = CreateThread (NULL, 0, (LPTHREAD_START_ROUTINE) NotifyThread,
                        0, 0, &dwID);

// Group Policy notification thread. 
// When the events are signaled, a message is added to the main window.

DWORD NotifyThread (DWORD dwDummy)
{
    HANDLE hHandles[3];
    DWORD dwResult;

    // This code assumes that hMachineEvent and hUserEvent
    // have been initialized.

    RegisterGPNotification(hMachineEvent, TRUE);
    RegisterGPNotification(hUserEvent, FALSE);

    hHandles[0] = hExit;
    hHandles[1] = hMachineEvent;
    hHandles[2] = hUserEvent;

    while (TRUE) 
    {
        dwResult = WaitForMultipleObjects (3, hHandles, FALSE, INFINITE);
        if ((dwResult == WAIT_FAILED) || ((dwResult - WAIT_OBJECT_0) == 0)) {
            if (dwResult == WAIT_FAILED) {
                AddString (TEXT("WaitForMultipleObjects failed."));
            }
            break;
        }

        if ((dwResult - WAIT_OBJECT_0) == 1) {
            AddString (TEXT("Machine notify event signaled."));
        } else {
            AddString (TEXT("User notify event signaled."));
        }
    }
    UnregisterGPNotification(hMachineEvent);
    UnregisterGPNotification(hUserEvent);

    return 0;
}