title: Windows 10 IOT Lifecycle (or: how to property terminate a background application)
body: <p>In order to use a UWP application on a headless Raspberry Pi 2 with Windows 10 IOT Core we can use the background application template which basically creates a new UWP app with just a background task that is executed on startup:</p>
  
  <pre><code>&lt;Extensions&gt;
    &lt;Extension Category="windows.backgroundTasks" EntryPoint="BackgroundApplication1.StartupTask"&gt;
      &lt;BackgroundTasks&gt;
        &lt;iot:Task Type="startup" /&gt;
      &lt;/BackgroundTasks&gt;
    &lt;/Extension&gt;
  &lt;/Extensions&gt;
  </code></pre>
  
  <p>In order to keep an application running, we can use the following startup code:</p>
  
  <pre><code>public void Run( IBackgroundTaskInstance taskInstance )
  {
    BackgroundTaskDeferral Deferral = taskInstance.GetDeferral();
  
    //Execute arbitrary code here.
  }
  </code></pre>
  
  <p>This way the application keeps running and the OS won't kill the app after any timeout in the IOT universe.</p>
  
  <p>So far, so great.</p>
  
  <p>However: I want to be able to properly close the background application when the device shuts down (or the application is asked to 'gently' close.</p>
  
  <p>In a 'normal' UWP application you can subscribe to the OnSuspending event.<br>
  How can I get a notification about an imminent shutdown / close in this background scenario?</p>
  
  <p>Help is greatly appreciated.<br>
  Thanks in advance!<br>
  -Simon</p>