UpdateTask for Desktop Bridge apps

We had a number of folks asking this question during //BUILD2017 at the Desktop Bridge booth, and today it came up again on Stackoverflow, so I decided to publish a sample and explain it here: How can I trigger some code when an update for my Win32/NET app has been deployed from the Windows Store?

tile
Source code on GitHub

Before I answer it, let me say that I think most apps won’t ever need to do this as they can run whatever new code they need to run when the app is launched the next time. However, for those cases where you do need to run code in the background automatically after the update has been deployed there is the UWP UpdateTask. If you have a packaged Win32/NET app that you brought to the Windows Store via Desktop Bridge, you can now utilize this task for your app in pretty much the same way as a regular UWP app would implement it. I have posted a WinForms app sample on GitHub – let me walk you through the details. First thing we need to do is extend our UWP app package with an extensions for the background task:

<Extensions>
  <Extension Category="windows.updateTask"
             EntryPoint="BackgroundTasks.UpdateTask" />
</Extensions>

To implement the background task we need to create a Windows Runtime component. So let’s add a project of that type to our solution in Visual Studio and name it “BackgroundTasks”:

addproject

All we need to do in this project is implement our UpdateTask class, derived from IBackgroundTask. For the sample we will just display the new package version in a toast notification:

namespace BackgroundTasks
{
    public sealed class UpdateTask : IBackgroundTask
    {
        public void Run(IBackgroundTaskInstance taskInstance)
        {
            PackageVersion version = Package.Current.Id.Version;
            string message = string.Format(
                "WinForms app updated to {0}.{1}.{2}.{3}",
                version.Major,
                version.Minor,
                version.Build,
                version.Revision);
            ToastHelper.ShowToast(message);
        }
    }
}

 

That’s all it takes to set this up. In case you need to execute some of your Win32/NET code outside of the UWP process that is hosting your task, you can do so using the FullTrustProcessLauncher API (not shown in this sample, but we have other samples for that API).

To see the sample in action, take the following steps:

  1. build and deploy the solution on your PC
  2. change the version number in the package manifest
  3. deploy the app again

Result: UpdateTask gets triggered and runs your code which displays the new version number

toast

Note: if you are not using Visual Studio/msbuild to build & package your own application, you will also need to add the “windows.activatableClass.inProcessServer” extension in your package manifest. VS/msbuild does this automatically for you, for any Windows Runtime components referenced by your app project – but if you need to do this manually check the appmanifest.xml file in the build output of the sample as reference.

12 thoughts on “UpdateTask for Desktop Bridge apps

  1. Thanks to share the valuable info.
    > change the version number in the package manifest
    Is this a mandatory one?
    I have experiences to try Update Task with my UWP(no bridge) app. In that case, it works when I’ve upload the package to store (package flight) and deploy. But, it was failed when I’ve tried with VS debug environment. I seems that … in VS case, I’ve keep the version number with no changed. If the version change is mandatory, I can understand the behavior.

    Like

  2. Hi,

    I can run your code without problems. The toast pops up as expected. Thanks!

    Now I would like to launch the Settings from the update background task. But the settings is not launched. Do you know why? I have tried other settings pages and tried to made it async, not working.

    public sealed class UpdateTask : IBackgroundTask
    {
    private void LaunchAnApp()
    {
    // The URI to launch
    string uriToLaunch = @”ms-settings:batterysaver”;

    // Create a Uri object from a URI string
    var uri = new Uri(uriToLaunch);

    Windows.System.Launcher.LaunchUriAsync(uri);
    }
    public void Run(IBackgroundTaskInstance taskInstance)
    {
    LaunchAnApp();

    // The show toast code was here. I replace it to launch the Settings.

    Like

    1. You shouldn’t launch a foreground experience from a background task. This would be confusing to the user, if the settings UX pops up out of nowhere. Therefore the LaunchUriAsync API requires foreground rights to work. One thing you could do (since you are a Desktop Bridge app), use the FullTrustProcessLauncher API to launch a fulltrust process from your task and then accomplish your scenario from there.

      Like

    1. Yes, you can but the registry write would go to the app-specific copy of the HKCU hive. The update wouldn’t be visible to anyone outside of your app. Would that satisfy your scenario?

      Like

  3. Apologies for not being clear. We’ve a WPF app which is packaged using desktop bridge. The WPF app has a reference to UpdateTask windows runtime component and it works as expected. We’re planning on migrating our WPF app to Net 5.0 and as per https://docs.microsoft.com/en-us/dotnet/core/compatibility/interop/5.0/built-in-support-for-winrt-removed, we can no longer consume WinMd files directly. I wanted to check if there’s a way of referencing the UpdateTask WinMD in Net 5.0 (a sample would be much appreciated).

    Like

Leave a comment