2

I'm developing a WinUI3 based app and I need to restart the app to apply some settings. But I can't find a function to do that. I noticed that there is a method--CoreApplication.RequestRestartAsync. However, it only shut the app down but didn't open the app. Is there an API can restart a WinUI app?

1
  • Are there any specifics in how your app is running? The reason I'm asking - the documentation mentions a couple of important things regarding this. For example, "The app must be visible and foreground when it calls this API." Commented Feb 15, 2024 at 4:37

2 Answers 2

1

Try the Restart API:

// You can pass arguments to the restarted instance.
private void RestartApp(string arguments = "")
{
    // The restart will be executed immediately.
    Windows.ApplicationModel.Core.AppRestartFailureReason failureReason = 
        Microsoft.Windows.AppLifecycle.AppInstance.Restart(arguments);

    // If the restart fails, handle it here.
    switch (failureReason)
    {
        case AppRestartFailureReason.RestartPending:
            break;
        case AppRestartFailureReason.NotInForeground:
            break;
        case AppRestartFailureReason.InvalidUser:
            break;
        default: //AppRestartFailureReason.Other
            break;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

The suggested Restart API is probably a good place to start.

You should also be able to start a new process like you would start any other .NET process:

string path = Process.GetCurrentProcess().MainModule.FileName;
Process process = Process.Start(new ProcessStartInfo(path));
IntPtr intPtr = IntPtr.Zero;
do
{
    intPtr = process.MainWindowHandle;
}
while (intPtr == IntPtr.Zero);

Environment.Exit(0);

2 Comments

This solution sometimes works, but sometimes it only close the app but don't run a new app
The code starts the process before closing the current one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.