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

# XUserRegisterForChangeEvent

> XUserRegisterForChangeEvent

# XUserRegisterForChangeEvent

Registers a callback for a user change event.

## Syntax

```cpp theme={null}
HRESULT XUserRegisterForChangeEvent(  
         XTaskQueueHandle queue,  
         void* context,  
         XUserChangeEventCallback* callback,  
         XTaskQueueRegistrationToken* token  
)  
```

### Parameters

*queue*   \_In\_opt\_\
Type: XTaskQueueHandle

A handle to the asynchronous queue to perform the operation on.

*context*   \_In\_opt\_\
Type: void\*

User defined context to pass to the callback.

*callback*   \_In\_\
Type: [XUserChangeEventCallback\*](/reference/system/xuser/functions/xuserchangeeventcallback)

User defined callback to register.

*token*   \_Out\_\
Type: [XTaskQueueRegistrationToken\*](/reference/system/xtaskqueue/structs/xtaskqueueregistrationtoken)

Contains a token identifying the registered callback. Use the token to un-register the callback.

### Return value

Type: HRESULT

HRESULT success or error code.\
For a list of error codes, see [Error Codes](/reference/errorcodes).

## Remarks

<Note>This function isn't safe to call on a time-sensitive thread. For more information, see [Time-sensitive threads](https://learn.microsoft.com/gaming/gdk/docs/gdk-dev/console-dev/overviews/threads/time-sensitive-threads).</Note>

The [XUserChangeEvent](/reference/system/xuser/enums/xuserchangeevent) enumeration specifies a change event type.
[XUserChangeEventCallback](/reference/system/xuser/functions/xuserchangeeventcallback) is a callback for a user change event.

To unregister a previously registered user change callback, call [XUserUnregisterForChangeEvent](/reference/system/xuser/functions/xuserunregisterforchangeevent).

The following example demonstrates how to handle user change events.

```cpp theme={null}
HRESULT RegisterForChanges()
{
    RETURN_HR_IF(E_UNEXPECTED, _token.token != 0);
    RETURN_IF_FAILED(XUserRegisterForChangeEvent(
        _queue,
        this,
        UserChangeEventHandler,
        &_token));
    return S_OK;
}

void UnregisterForChanges()
{
    XUserUnregisterForChangeEvent(_token, false);
    _token.token = 0;
}

void UserChangeEventHandler(
    XUserLocalId userLocalId,
    XUserChangeEvent event)
{
    auto iter = std::find_if(
        _users.begin(),
        _users.end(),
        [&userLocalId](const User& candidate)
    {
        XUserLocalId candidateUserLocalId;
        XUserGetLocalId(candidate.Handle(), &candidateUserLocalId);
        return candidateUserLocalId == userLocalId;
    });

    // User not known
    if (iter == _users.end())
    {
        return;
    }

    auto handle = iter->Handle();

    // If a guest gets signed out, immediately close the handle
    bool isGuest;
    if (SUCCEEDED_LOG(XUserGetIsGuest(handle, &isGuest)) &&
        isGuest &&
        event == XUserChangeEvent::SignedOut)
    {
        _users.erase(iter);
    }

    if (event == XUserChangeEvent::SigningOut)
    {
        // Delay the user signing out just for fun
        XUserSignOutDeferralHandle deferral;
        if (SUCCEEDED_LOG(XUserGetSignOutDeferral(&deferral)))
        {
            // Hold the deferral for 5 seconds then close it
            std::thread completeDeferralThread(
                [deferral]()
            {
                std::this_thread::sleep_for(std::chrono::milliseconds(5000));
                XUserCloseSignOutDeferralHandle(deferral);
            });

            completeDeferralThread.detach();
        }
    }

    if (event == XUserChangeEvent::GamerPicture)
    {
        iter->LoadGamerPicAsync(_queue);
    }
}
```

## Requirements

**Header:** XUser.h

**Library:** xgameruntime.lib

**Supported platforms:** Windows, Steam Deck, XBOX One family consoles and XBOX Series consoles

## Conceptual documentation

* [Time-sensitive threads](https://learn.microsoft.com/gaming/gdk/docs/gdk-dev/console-dev/overviews/threads/time-sensitive-threads)
* [Implement player sign-in in your game](https://learn.microsoft.com/gaming/gdk/docs/gdk-dev/pc-dev/tutorials/pc-e2e-guide/e2e-services/e2e-user-sign-in)

## See also

[XUser](/reference/system/xuser/xuser_members)

[XUserChangeEvent](/reference/system/xuser/enums/xuserchangeevent)

[XUserChangeEventCallback](/reference/system/xuser/functions/xuserchangeeventcallback)

[XUserUnregisterForChangeEvent](/reference/system/xuser/functions/xuserunregisterforchangeevent)
