How do I make my app exit when the user updates it via the Microsoft store?

sysadmin 40 Reputation points
2025-11-10T00:54:57.24+00:00

I have an app in the Microsoft Store, when an update is available a user can click Update in the store.

I added the following code to detect that and allow the store to apply the update:


            var catalog = Windows.ApplicationModel.PackageCatalog.OpenForCurrentPackage();
            catalog.PackageUpdating += (Windows.ApplicationModel.PackageCatalog sender, Windows.ApplicationModel.PackageUpdatingEventArgs args) =>
            {
                Environment.Exit(0);
            };

However, this doesn't work the first time user clicks update, instead an error appears "This file is in use 0x8000...". When the user clicks Retry, the app updates succesfully.

2 questions:

  1. How can I make the Microsoft Store update work the first time without the user having to click Retry
  2. How can I make the app automatically start after it has been updated via Microsoft Store?
Windows development | Windows App SDK
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Raymond Huynh (WICLOUD CORPORATION) 3,635 Reputation points Microsoft External Staff Moderator
    2025-11-10T09:20:03.0966667+00:00

    Hello sysadmin,

    Your issue with Environment.Exit(0) is similiar to a discussed thread:
    https://learn.microsoft.com/en-us/answers/questions/1165596/how-should-a-service-exit-to-prevent-an-error

    The thread shows that Environment.Exit() can cause issues because it ends the process too fast.

    I recommend try these instead: 

    • Process.GetCurrentProcess().Kill() - more forceful termination 

    https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.process.getcurrentprocess?view=net-9.0 

    • Application.Current.Exit() - proper WinUI 3 shutdown 

    https://learn.microsoft.com/en-us/uwp/api/windows.ui.xaml.application.exit?view=winrt-20348 

    For the auto-restart part, you can use the AppInstance.Restart API (https://learn.microsoft.com/en-us/windows/apps/windows-app-sdk/applifecycle/applifecycle-restart) from the Windows App SDK. Here's an example you can add to your PackageInstalling event handler:

    catalog.PackageInstalling += (sender, args) =>
    
    {
    
        if (args.IsComplete)
    
        {
    
            // Restart the app after update is finished
    
            AppInstance.Restart("");
    
        }
    
    };
    

    This method is the most straightforward for a WinUI 3 app packaged for the Microsoft Store, as it handles the restart process for you. If you run into any issues with AppInstance.Restart, a reliable fallback is to use Process.Start to launch a new instance of your application before the current one shuts down.

    Hope this helps!


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.