locked
What happens when an IBackgroundTask is killed by Windows

    Question

  • So my scenario is this. My Windows Store app has a background task which uploads any data which the main app could not complete (e.g. it was suspended mid upload). As both the background task and the foreground app can both upload the same data I have to sync the two processes. I do this by using a Semaphore (I can't use Mutex since the task uses 'await/async'). 

    This works well, but I am concerned as to what happens if Windows decides to kill the background task while it is in the middle of uploading (the docs state that the process is limited to 1 sec CPU time). i.e. would the Semaphore get released? The general structure of the code is as follows:

    async public void Run(IBackgroundTaskInstance taskInstance) { BackgroundTaskDeferral deferral = taskInstance.GetDeferral(); Semaphore uploadSemaphore; if (!Semaphore.TryOpenExisting("UploadSemaphore", out uploadSemaphore)) uploadSemaphore = new Semaphore(1, 1, "UploadSemaphore");

    uploadSemaphore.WaitOne();

    try { // upload data await ... } finally { uploadSemaphore.Release(); } deferral.Complete(); }


    So would finally get called if the background task is terminated for some reason (e.g. it exceeds some limit)?

    I guess this question also applies to Windows Phone and the ScheduledTaskAgent, which is the equivalent of the IBackgroundTask, which has 25 seconds to complete its work.




    • Edited by doogledog Tuesday, July 1, 2014 10:18 AM
    Tuesday, July 1, 2014 10:14 AM

Answers

  • Shouldn't you be using a BackgroundTransfer to avoid this issue?  I would not depend on the Semaphore being signaled if the process dies, but you should use a timeout value in order to unblock the process in the case that it does.

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    • Marked as answer by doogledog Wednesday, July 2, 2014 2:12 PM
    Tuesday, July 1, 2014 7:16 PM
    Moderator

All replies

  • Shouldn't you be using a BackgroundTransfer to avoid this issue?  I would not depend on the Semaphore being signaled if the process dies, but you should use a timeout value in order to unblock the process in the case that it does.

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    • Marked as answer by doogledog Wednesday, July 2, 2014 2:12 PM
    Tuesday, July 1, 2014 7:16 PM
    Moderator
  • Hi Matt,

    The uploads are actually Dropbox uploads using the Dropbox 'Core API', so I am not sure (though I don't know for definite) that the BackgroundTransfer API is going to be able to handle that. I did have a bash with the BackgroundTransfer previously but could not get it to work with Dropbox. If it was possible then this would be a preferred solution.

    If not, then I will just have to code in a workaround. I do use timeouts in my code already (so the app will not hang), but the problem will come if the IBackgroundTask does not let go of the Semaphore, which will prevent both the foreground app and future runs of the background task from doing any uploading. I can get around this by fully releasing the semaphore when the foreground app is suspended (there may be some edge cases where this would be dangerous, but would probably work 99.999% of the time).

    Wednesday, July 2, 2014 11:13 AM
  • Ok, you inspired me to try again with the BackgroundUploader. This time I managed to get it working with uploading files to Dropbox. So I now have an alternative to using a BackgroundTask, which is good.
    • Edited by doogledog Wednesday, July 2, 2014 2:11 PM
    Wednesday, July 2, 2014 2:10 PM
  • Glad to hear it. :-)

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Wednesday, July 2, 2014 2:13 PM
    Moderator