Search code examples
c#uwplaunch

Wait for an launched application to stop


We are currently working on a project in UWP where we have to start an external application to modify some documents.

We looked into the Windows.System.Launcher API but it seems that we need more than what it can offer us.

As we launched the application from a file, we use the LaunchFileAsync method, based on the example given by the MSDN :

async void DefaultLaunch()
{
   // Path to the file in the app package to launch
   string imageFile = @"images\test.png";

   var file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(imageFile);

   if (file != null)
   {
      // Launch the retrieved file
      var success = await Windows.System.Launcher.LaunchFileAsync(file);

      if (success)
      {
         // File launched
      }
      else
      {
         // File launch failed
      }
   }
   else
   {
      // Could not find file
   }
}

So far, the example suit us well but we also need to be warned when the user is done with the file. The best would be to be able to give the launcher a callback method.

We haven't found anything like that yet in the documentation. Is this even possible ? Do we need to use another solution ?

TL;DR : Is there a solution to open another application from a UWP app and wait for it to return a result object ?


Solution

  • If the external app is also a UWP app then Launcher.LaunchUriForResultsAsync is designed for this. It will launch the target app then wait for the app to call back with the results.

    See Launch an app for results for a full walkthrough of how this works.

    If the target app isn't a UWP app then you can implement the same thing yourself: both apps declare a protocol. The client launches the server with the server's protocol. When the server's done it notifies the caller by launching the client's protocol.

    You might also want to look into App Services which allow a UWP server app to expose a REST-like service to clients on the local system.