title: Registering a background task in WinRT
body: <p>I'm trying to register a background task and I'm getting some strange behaviour.  I appears that the task itself is registering and firing at the right time; however, when it does fire it's closing my program down (with no error).  </p>
  
  <p>I suspect that the reason is linked to the fact that the program is not asking me is I want to allow a background task to run when I launch.  I have created a declaration for the background task as a system event, and am registering like this from the App.Xaml.cs:</p>
  
  <pre><code>var builder = new BackgroundTaskBuilder();
  
  builder.Name = "NewTask";
  builder.TaskEntryPoint = "ConnectionMonitor.CheckInternet";
  builder.SetTrigger(new SystemTrigger(SystemTriggerType.InternetAvailable, false));
  
  BackgroundTaskRegistration task = builder.Register();
  </code></pre>
  
  <p>So, I believe my question is: why would it not ask me for permission to run a background task (which I assume will give me the answer to my main problem of why it is crashing)?</p>
  
  <p>I'm not sure whether it matters, but this is a Windows Universal app (the app.xaml.cs above is in the <code>Shared</code> project.</p>
  
  <p>The task looks like this:</p>
  
  <pre><code>BackgroundTaskDeferral _deferral = taskInstance.GetDeferral();
  
  bool newConnected = IsConnected();
  if (connected != newConnected)
  {
      connected = newConnected;
      var notifier = ToastNotificationManager.CreateToastNotifier();
      var template = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText02);
  
      var element = template.GetElementsByTagName("text")[0];
      element.AppendChild(template.CreateTextNode(connected ? "Connection available" : "Connection lost"));
  
      var toast = new ToastNotification(template);
      notifier.Show(toast);  
  }
  
  _deferral.Complete();
  </code></pre>