Sciencekill
Sciencekill

Reputation: 317

Is there a function that can restart a WinUI app?

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?

Upvotes: 2

Views: 373

Answers (2)

mm8
mm8

Reputation: 169360

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);

Upvotes: 0

Andrew KeepCoding
Andrew KeepCoding

Reputation: 13641

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;
    }
}

Upvotes: 1

Related Questions