Restart An App Programmatically In UWP (Windows 10 Fall Creators Update Features)

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

  1. CoreApplication.RequestRestartAsync(string launchArguments)
  2. 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 
  1. <Page  
  2.     x:Class="App1.MainPage"  
  3.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  4.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  5.     xmlns:local="using:App1"  
  6.     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
  7.     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
  8.     mc:Ignorable="d">  
  9.   
  10.     <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" Margin="10,10,10,10">  
  11.         <Grid.RowDefinitions>  
  12.             <RowDefinition Height="Auto"/>  
  13.             <RowDefinition Height="Auto"/>  
  14.             <RowDefinition Height="Auto"/>  
  15.         </Grid.RowDefinitions>  
  16.           
  17.         <TextBlock Name="TxtStatus" Grid.Row="0" Text="" FontSize="25" TextWrapping="WrapWholeWords"/>  
  18.         <Button Grid.Row="1" Content="Restart" Click="ButtonBase_OnClick" Margin="10,10,10,10"></Button>  
  19.     </Grid>  
  20. </Page>  
Code behinde file 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.IO;  
  4. using System.Linq;  
  5. using System.Runtime.InteropServices.WindowsRuntime;  
  6. using Windows.ApplicationModel.Activation;  
  7. using Windows.ApplicationModel.Core;  
  8. using Windows.Foundation;  
  9. using Windows.Foundation.Collections;  
  10. using Windows.System;  
  11. using Windows.UI.Popups;  
  12. using Windows.UI.Xaml;  
  13. using Windows.UI.Xaml.Controls;  
  14. using Windows.UI.Xaml.Controls.Primitives;  
  15. using Windows.UI.Xaml.Data;  
  16. using Windows.UI.Xaml.Input;  
  17. using Windows.UI.Xaml.Media;  
  18. using Windows.UI.Xaml.Navigation;  
  19.   
  20. // The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409  
  21.   
  22. namespace App1  
  23. {  
  24.     /// <summary>  
  25.     /// An empty page that can be used on its own or navigated to within a Frame.  
  26.     /// </summary>  
  27.     public sealed partial class MainPage : Page  
  28.     {  
  29.         public MainPage()  
  30.         {  
  31.             this.InitializeComponent();  
  32.   
  33.             TxtStatus.Text = "Restart Application Sample";  
  34.   
  35.             if (ReadAppData.ArgInfo != null)  
  36.                 TxtStatus.Text = ReadAppData.ArgInfo;  
  37.         }  
  38.   
  39.           
  40.         private async void ButtonBase_OnClick(object sender, RoutedEventArgs e)  
  41.         {  
  42.             var result = await CoreApplication.RequestRestartAsync("Application Restart Programmatically ");  
  43.   
  44.             if (result == AppRestartFailureReason.NotInForeground ||  
  45.                 result == AppRestartFailureReason.RestartPending ||  
  46.                 result == AppRestartFailureReason.Other)  
  47.             {  
  48.                 var msgBox = new MessageDialog("Restart Failed", result.ToString());  
  49.                 await msgBox.ShowAsync();  
  50.             }  
  51.         }      
  52.           
  53.     }  
  54. }  
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. 
  1. private async void BtnUser_OnClick(object sender, RoutedEventArgs e)  
  2.         {  
  3.             var userList = await User.FindAllAsync();  
  4.   
  5.             var lst = new List<string>()  
  6.             {  
  7.                 KnownUserProperties.FirstName  
  8.             };  
  9.   
  10.             foreach (var user in userList)  
  11.             {  
  12.                 var accountName =await user.GetPropertiesAsync(lst);  
  13.   
  14.                 if (accountName != null)  
  15.                 {  
  16.                     var name = accountName[KnownUserProperties.FirstName].ToString();  
  17.                     if (name == "Vinoth")  
  18.                     {  
  19.                         AppRestartFailureReason result =  
  20.                             await CoreApplication.RequestRestartForUserAsync(user, "user based Restart");   
  21.   
  22.                     }  
  23.                 }    
  24.             }    
  25.         }  
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. 
  1. protected override void OnLaunched(LaunchActivatedEventArgs e)  
  2.         {  
  3.             Frame rootFrame = Window.Current.Content as Frame;  
  4.   
  5.             // Do not repeat app initialization when the Window already has content,  
  6.             // just ensure that the window is active  
  7.             if (rootFrame == null)  
  8.             {  
  9.                 // Create a Frame to act as the navigation context and navigate to the first page  
  10.                 rootFrame = new Frame();  
  11.   
  12.                 rootFrame.NavigationFailed += OnNavigationFailed;  
  13.   
  14.                 if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)  
  15.                 {  
  16.                     //TODO: Load state from previously suspended application  
  17.                 }  
  18.   
  19.                 // Place the frame in the current Window  
  20.                 Window.Current.Content = rootFrame;  
  21.             }  
  22.   
  23.             switch (e.Kind)  
  24.             {  
  25.                 case ActivationKind.Launch:  
  26.                 {  
  27.                     string lanuch = e.Arguments;  
  28.                     if (!string.IsNullOrEmpty(lanuch))  
  29.                     {  
  30.                         ReadAppData.ArgInfo = "Application Last State : " + e.PreviousExecutionState.ToString();  
  31.                         ReadAppData.ArgInfo += Environment.NewLine + "Lanuch Arguments : " + lanuch;  
  32.                     }  
  33.   
  34.                 }  
  35.                 break;  
  36.             }  
  37.           
Conclusion

I hope you understood how to use Restart API.

Up Next
    Ebook Download
    View all
    Learn
    View all