title: Correct implementation when using background safe task together with async/await
body: <p>If the user presses the home button or is opening the app switcher or is receiving an incoming call a currently running task (web request) should get finished in background. I'm not using <code>NSURLSession</code> for this because I don't want to change the currently underlying class now. Therefore i want to make use of <a href="http://developer.xamarin.com/guides/cross-platform/application_fundamentals/backgrounding/part_3_ios_backgrounding_techniques/ios_backgrounding_with_tasks/" rel="nofollow">background-safe tasks</a>.</p>
  
  <p>I tried this:</p>
  
  <pre><code>public async Task retrieveCarList(){
      backgroundTaskID = UIApplication.SharedApplication.BeginBackgroundTask (() =&gt; {
          if(backgroundTaskID != 0){
              UIApplication.SharedApplication.EndBackgroundTask(backgroundTaskID);
              backgroundTaskID = 0;
          }
      });
  
      List&lt;Car&gt; carList = await manager.GetCarList();
      if(carList != null) {
          this.list = carList;
      } else {
          new UIAlertView("Error occurred", "", null, "OK", null).Show();
      }
  
      if(backgroundTaskID != 0){
          UIApplication.SharedApplication.EndBackgroundTask(backgroundTaskID);
          backgroundTaskID = 0;
      }
  }
  </code></pre>
  
  <p>This seems to work, but I have to test it more.</p>
  
  <p><strong>Questions</strong>:</p>
  
  <ol>
  <li><p>Can I use <code>BeginBackgroundTask</code>/<code>EndBackgroundTask</code> everywhere in my application? Most <a href="https://github.com/xamarin/monotouch-samples/blob/master/BackgroundExecution/AppDelegate.cs" rel="nofollow">examples</a> only use this in <code>DidEnterBackground</code> ...</p></li>
  <li><p>How does the system know where the beginning and the end of this background thread is?</p></li>
  <li><p>Is the code between <code>BeginBackgroundTask</code> and <code>EndBackgroundTask</code> executed even UI related stuff is used? </p></li>
  <li><p>Is this the correct implementation after the blog post <a href="http://software.tavlikos.com/2010/12/08/multitasking-in-ios-the-monotouch-way-part-i/" rel="nofollow">
  Multitasking in iOS: the MonoTouch way – Part I: Pressing the Home button</a> but with <code>async</code>/<code>await</code>?</p></li>
  </ol>
  
  <p><strong>Edit 1:</strong></p>
  
  <p>Now I tried to work with <code>Console.WriteLine</code>. It seems that the <code>await</code> expression is executed but the code afterwards will be only executed if the user is opening the app again. What I'm worrying about is that <code>EndBackgroundTask</code> is only executed if the user is back at the app. Therefore the app could get killed if <code>EndBackgroundTask</code> is not called.</p>
  
  <p>On the other side if I'm not using the background safe task the app is not doing the web request in background anymore if the home button is pressed. So it kind of works ...</p>
  
  <p>How can I put all lines of code in the <code>await</code> expression? Then all code should get executed or?</p>
  
  <p><strong>Edit 2:</strong></p>
  
  <p>Putting the <code>await</code> expression in between an <code>System.Action</code> causes the app to crash and no error is shown. I also tried to use <code>BeginBackgroundTask</code> in <em>retrieveCarList()</em> and use <code>EndBackgroundTask</code> in the awaited <em>GetCarList()</em>, but the code is only executed if I'm back in the app. Additionally, I tried to wrap my <code>await</code> in another <code>Task.Run</code>, but this brings problems because I'm accessing UI related stuff which is not possible with this structure. My first try still seems to be the best, but it has the disadvantage stated above. If someone has a better implementation please post it.</p>