A long-awaited feature has come to Universal Windows programming; i.e.; the ability to programmatically restart the application. Previously we had to inform to the user with a prompt (like Message Dialog) to close and relaunch the application. Now relaunch is not required from the user side.
This article explains about how to implement this feature into our application.
Note
This API will be support Windows Insider Build version 16257 or greater, public version will be available on 17-Oct-17 (windows 10 fall creators update version) onwards
System Requirements
- Windows 10 Insider Build 16257 or greater
- Windows 10 16257 SDK
- Visual Studio 2017
Restart API
RequestRestartAsync & RequestRestartForUserAsync API
RequestRestartAsync & RequestRestartForUserAsync is used to Restart the application, this API is available in CoreApplication class
- CoreApplication.RequestRestartAsync(string launchArguments)
- CoreApplication. RequestRestartForUserAsync (User user,string launchArguments)
RequestRestartAsync (string launchArguments)
This API is used to restart the application normally; there's no need to check any condition. This API contains one argument.
launchArguments
This argument is used to pass the information to the Restart Instance. Let see one simple example, how to restart the application programmatically and read the
launchArguments in the new Instance
Open VisualStudio -> File -> Visual C# -> Windows Universal -> Blank App(Universal Windows)
Select the Target Version ->
16257 or greater ( This sample has developed by 16267 version)
Xaml Code
- <Page
- x:Class="App1.MainPage"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:local="using:App1"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- mc:Ignorable="d">
-
- <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" Margin="10,10,10,10">
- <Grid.RowDefinitions>
- <RowDefinition Height="Auto"/>
- <RowDefinition Height="Auto"/>
- <RowDefinition Height="Auto"/>
- </Grid.RowDefinitions>
-
- <TextBlock Name="TxtStatus" Grid.Row="0" Text="" FontSize="25" TextWrapping="WrapWholeWords"/>
- <Button Grid.Row="1" Content="Restart" Click="ButtonBase_OnClick" Margin="10,10,10,10"></Button>
- </Grid>
- </Page>
Code behinde file
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Runtime.InteropServices.WindowsRuntime;
- using Windows.ApplicationModel.Activation;
- using Windows.ApplicationModel.Core;
- using Windows.Foundation;
- using Windows.Foundation.Collections;
- using Windows.System;
- using Windows.UI.Popups;
- using Windows.UI.Xaml;
- using Windows.UI.Xaml.Controls;
- using Windows.UI.Xaml.Controls.Primitives;
- using Windows.UI.Xaml.Data;
- using Windows.UI.Xaml.Input;
- using Windows.UI.Xaml.Media;
- using Windows.UI.Xaml.Navigation;
-
-
-
- namespace App1
- {
-
-
-
- public sealed partial class MainPage : Page
- {
- public MainPage()
- {
- this.InitializeComponent();
-
- TxtStatus.Text = "Restart Application Sample";
-
- if (ReadAppData.ArgInfo != null)
- TxtStatus.Text = ReadAppData.ArgInfo;
- }
-
-
- private async void ButtonBase_OnClick(object sender, RoutedEventArgs e)
- {
- var result = await CoreApplication.RequestRestartAsync("Application Restart Programmatically ");
-
- if (result == AppRestartFailureReason.NotInForeground ||
- result == AppRestartFailureReason.RestartPending ||
- result == AppRestartFailureReason.Other)
- {
- var msgBox = new MessageDialog("Restart Failed", result.ToString());
- await msgBox.ShowAsync();
- }
- }
-
- }
- }
Output
RequestRestartForUserAsync (User user, string launchArguments)
This API is used to enable user based conditions to restart the application. The first argument is User account; this argument is used to check the condition. For example, if the login user matches then it calls the restart API and the second argument is launchArguments which is used to the pass the information to the Restart Instance.
- private async void BtnUser_OnClick(object sender, RoutedEventArgs e)
- {
- var userList = await User.FindAllAsync();
-
- var lst = new List<string>()
- {
- KnownUserProperties.FirstName
- };
-
- foreach (var user in userList)
- {
- var accountName =await user.GetPropertiesAsync(lst);
-
- if (accountName != null)
- {
- var name = accountName[KnownUserProperties.FirstName].ToString();
- if (name == "Vinoth")
- {
- AppRestartFailureReason result =
- await CoreApplication.RequestRestartForUserAsync(user, "user based Restart");
-
- }
- }
- }
- }
Output
Restart Failure
Both API return the APPRestartFailureReason if the Restart API get failed ,
Failed reason may be one of the below reasons.
- AppRestartFailureReason.NotInForeground
- AppRestartFailureReason.RestartPending
- AppRestartFailureReason.Other
Read the Arguments
Both Restart API and launchArguments should be passed. Once the application gets restarted, this value reads from the OnLaunched function.
First we need to check ActionKind.Launch , then read the value from LaunchedActivatedEventArgs.
- protected override void OnLaunched(LaunchActivatedEventArgs e)
- {
- Frame rootFrame = Window.Current.Content as Frame;
-
-
-
- if (rootFrame == null)
- {
-
- rootFrame = new Frame();
-
- rootFrame.NavigationFailed += OnNavigationFailed;
-
- if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
- {
-
- }
-
-
- Window.Current.Content = rootFrame;
- }
-
- switch (e.Kind)
- {
- case ActivationKind.Launch:
- {
- string lanuch = e.Arguments;
- if (!string.IsNullOrEmpty(lanuch))
- {
- ReadAppData.ArgInfo = "Application Last State : " + e.PreviousExecutionState.ToString();
- ReadAppData.ArgInfo += Environment.NewLine + "Lanuch Arguments : " + lanuch;
- }
-
- }
- break;
- }
-
Conclusion
I hope you understood how to use Restart API.