3

So I made a simple Windows Phone 8 app that uploads a text file to the user's SkyDrive account. My code works fine while my app is running in the foreground, but when I attempt to upload a text file when my app is closing , it doesn't seem to work.

I'm using the Live Connect SDK v5.3 for WP8. SDK link: http://msdn.microsoft.com/en-us/library/live/hh826550.aspx

I'm using this piece of code to do the background upload when my app closes (when the user hits "back button" on their phone:

protected override void OnBackKeyPress(CancelEventArgs e)
{
    SaveSkyDriveData();
    base.OnBackKeyPress(e);
}

public async Task SaveSkyDriveData()
{
   var res = await client.BackgroundUploadAsync("me/skydrive", new Uri("/shared/transfers/MyData.txt", UriKind.RelativeOrAbsolute), OverwriteOption.Overwrite);
}

Any ideas why this code doesn't work when the app is closing? I've read through the SDK that says this should work even after the app has been dismissed. Here's the SDK link for uploading files in the background: http://msdn.microsoft.com/en-us/library/live/hh826531.aspx#uploading_files

Thanks!

FrankieA
  • 278
  • 1
  • 8
  • Did you subscribe to all of the events as stated in the doc? "1.In the code that initializes the LiveConnectClient variable, declare handlers for the BackgroundUploadCompleted event ... Then call the AttachPendingTransfers method, which checks for pending background file transfers that were started in a previous instance of the app" Alternatively you can try [this suggestion](http://visuallylocated.com/post/2013/06/14/Performing-an-async-operation-in-the-OnBackKeyPress-method-and-still-exit-your-Windows-Phone-apps.aspx) – Shawn Kendrot Jul 04 '13 at 06:11
  • So I was confused by that doc as well. None of those events were available to me. After some digging, it seems that they took out those events for projects that are targeted for WP8 only. If I create a new project targeting WP7.1, those events are available. Maybe it has to do with the new async/await model for WP8? Alternatively, I tried to synchronous-blocking OnBack method - and that works... but I I really want my app to close while the upload happens. Thanks though! – FrankieA Jul 04 '13 at 22:36
  • Do you have to target WP8? Or can you target WP7. There are still a lot of WP7 devices out there. The link I suggested should work as an alternative – Shawn Kendrot Jul 05 '13 at 06:01

2 Answers2

0

You cannot upload files during the closing of the app in WP as you only have about 10 seconds to save state before it's shutdown

You might be able to do it during de-activation but it would be a push as the timescales are the same.

A better solution would be to have a background task (scheduled task) that runs and checks for files to upload and does so periodically.

Another alternative depending on your use case would be to use the parse SDK rather than upload to SkyDrive unless there is a specific reason the file needs to be hosted on SkyDrive

Hope this helps

Darkside
  • 1,722
  • 14
  • 19
  • Interesting, where did you get this information (regarding not be able to upload files during closing)? If you look at the SDK link (here: http://msdn.microsoft.com/en-us/library/live/hh826531.aspx#uploading_files) it says, and I quote "For Windows Phone 7.5 apps, you can upload files even if the app that started the upload is suspended or quits. Here's how." - and it lists the exact same method that I'm using to do the upload. If what you're saying is true, then I wonder why they have that in the SDK. Thanks for replying! – FrankieA Jul 04 '13 at 22:43
  • Well background tasks are part of the framework and hence you don't need to control them but you do need the time to instigate the process. Just not enough time in closing but I would test with "deactivating" if you must do while the app is closing / being suspended. Best solution though is to do it completely offline in a background task. It's there in the SDK to point out that it is a framework process which your app can integrate with. – Darkside Jul 08 '13 at 13:47
0

To revive an ancient thread, is this because you aren't awaiting your async task?

protected override **async** void OnBackKeyPress(CancelEventArgs e)
{
    **await** SaveSkyDriveData();
    base.OnBackKeyPress(e);
}

the compiler should be warning you that nothing is awaiting the task... so nothing downstream knows that there's work in progress? so any async work that started probably doesn't complete before the app closes.

if that's related, there are other answers about waiting synchronously as well, like using Task.Run(() => SaveSkyDriveData()).Wait(); to make the async thing be synchronous,

John Gardner
  • 24,225
  • 5
  • 58
  • 76