locked
WinRT suspend/resume bug or expected behaviour? RRS feed

  • Question

  • Developing my first app, I need to use a maintenance task to download some data, and then reload the data and refresh the display when the app resumes, but I noticed some strange behaviour which I confirmed using toasts.

    Basically, the app would suspend (and save the data and register the background task) fine, then the background task would start after a bit, grab the data and save it as planned.

    However, after this the resume and suspend events would fire in quick succession, despite the app staying in the background suspended the whole time.  As I was using these events to save and load data it was messing things up a bit.

    I confirmed this with toasts as it enabled me to close Visual Studio completely to exclude it from the test.


    • Edited by sylian.eu Monday, August 26, 2013 1:28 PM
    Monday, August 26, 2013 1:28 PM

Answers

  • After spending the afternoon experimenting I have found out some things.

    Firstly, it was my fault.  Inside my background task class I had a method which returned a Task and it seems I possibly messed something up.  The following code was enough to cause it to happen:

    public async void Run(IBackgroundTaskInstance taskInstance)
    {
      taskInstance.Canceled += new           
      BackgroundTaskCanceledEventHandler(OnCanceled);
      if (_cancelRequested) return;
      var deferral = taskInstance.GetDeferral();
      await Test();
      deferral.Complete;
    }
    
    
    private Task Test()
    {
      return null;
    }

    I found out I don't need to do anything awaitable there so I changed it to return void and it works as expected.

    So, it seems that that code was causing an exception and it was this exception that was resuming and suspending the main app.  I'm not sure whether that is how it should be or not though but at least I can avoid it now.

    • Marked as answer by Anne Jing Monday, September 2, 2013 1:16 AM
    Monday, August 26, 2013 7:55 PM

All replies

  • Can you post a repro and instructions to duplicate this?  SkyDrive works well for that.

    Jeff Sanders (MSFT)

    @jsandersrocks - Windows Store Developer Solutions @WSDevSol
    Getting Started With Windows Azure Mobile Services development? Click here
    Getting Started With Windows Phone or Store app development? Click here
    My Team Blog: Windows Store & Phone Developer Solutions
    My Blog: Http Client Protocol Issues (and other fun stuff I support)

    Monday, August 26, 2013 7:09 PM
    Moderator
  • After spending the afternoon experimenting I have found out some things.

    Firstly, it was my fault.  Inside my background task class I had a method which returned a Task and it seems I possibly messed something up.  The following code was enough to cause it to happen:

    public async void Run(IBackgroundTaskInstance taskInstance)
    {
      taskInstance.Canceled += new           
      BackgroundTaskCanceledEventHandler(OnCanceled);
      if (_cancelRequested) return;
      var deferral = taskInstance.GetDeferral();
      await Test();
      deferral.Complete;
    }
    
    
    private Task Test()
    {
      return null;
    }

    I found out I don't need to do anything awaitable there so I changed it to return void and it works as expected.

    So, it seems that that code was causing an exception and it was this exception that was resuming and suspending the main app.  I'm not sure whether that is how it should be or not though but at least I can avoid it now.

    • Marked as answer by Anne Jing Monday, September 2, 2013 1:16 AM
    Monday, August 26, 2013 7:55 PM