Udostępnij za pośrednictwem


CoreApplication.RequestRestartForUserAsync(User, String) Method

Definition

Restart the app in the context of a different user.

public:
 static IAsyncOperation<AppRestartFailureReason> ^ RequestRestartForUserAsync(User ^ user, Platform::String ^ launchArguments);
/// [Windows.Foundation.Metadata.RemoteAsync]
 static IAsyncOperation<AppRestartFailureReason> RequestRestartForUserAsync(User const& user, winrt::hstring const& launchArguments);
[Windows.Foundation.Metadata.RemoteAsync]
public static IAsyncOperation<AppRestartFailureReason> RequestRestartForUserAsync(User user, string launchArguments);
function requestRestartForUserAsync(user, launchArguments)
Public Shared Function RequestRestartForUserAsync (user As User, launchArguments As String) As IAsyncOperation(Of AppRestartFailureReason)

Parameters

user
User

The user to restart the app as.

launchArguments
String

Platform::String

winrt::hstring

The arguments to pass to the restarted instance.

Returns

The status of the restart request.

Attributes

Windows requirements

Device family
Windows 10 Fall Creators Update (introduced in 10.0.16299.0)
API contract
Windows.Foundation.UniversalApiContract (introduced in v5.0)

Examples

private async void RestartAsAnotherUser(string selectedUser)
{
    IReadOnlyList<User> users = await User.FindAllAsync();
    foreach (User user in users)
    {
        object accountProperty =
            await user.GetPropertyAsync(KnownUserProperties.AccountName);
        string accountName = (string)accountProperty;
        if (accountName == selectedUser)
        {
            // Attempt restart, with arguments.
            AppRestartFailureReason result = await App.RequestRestartForUserAsync(
                user, "-fastInit -mainMenu");

            // Restart request denied, tell the user to restart manually.
            if (result== AppRestartFailureReason.InvalidUser)
            {
                SendToast(string.Format("Could not restart as user {0} - please manually restart.", user));
            }
        }
    }
}

Remarks

  • The app must be visible and foreground when it calls this API.
  • If the restart fails, but the user subsequently launches the app manually, the app will launch normally and no restart arguments will be passed.
  • If the app wasn't launched in the normal way, but was launched by a share contract, file picker, app-service, and so on, the app should not call this API because the user will not expect the resulting behavior.
  • The app is responsible for ensuring the validity of the User object. The activation will fail if there is policy in place which prevents that user from executing the app.
  • The app should not request an Extended Execution session after it has called this API because that will result in a poor user experience.
  • If the app has any in-process background tasks running when it calls this API, those tasks will be cancelled in the normal way. Out-of-process background tasks will not be affected.
  • When the app is restarted, LaunchActivatedEventArgs.PreviousExecutionState will have the value Terminated so that the app can distinguish between a resume and a restart.

Applies to

See also