locked
Some Queries on Suspending a JavaScript Windows Store App RRS feed

  • Question

  • Hello there

    I have some queries on suspending a JavaScript based Windows store app, as follows:

    1)
    I am using the "checkpoint" event to catch the suspension, i.e:

    WinJs.Application.addEventListener("checkpoint", onCLSuspend);

    function onCLSuspend(eventArgs) {
    }

    The documentation states that I need to use "getDeferral()" or "SetPromise()" to run an asyncronous operation, however I can’t find any example or sample to show how to do this in conjunction with the "checkpoint" event. How should I implement this?

    2)
    Can the onCLSuspend() function be called whilst my main thread is already running and in the middle of some application code? (especially if my main thread is implementing some asyncronous functionality).

    3)
    My application might be in the middle of a background process using a web worker when I receive the suspend notification, and I may want to perform additional background tasks within onCLSuspend(). The web worker has a different model for performing asyncronous operations (you set it off and it will call back one of your functions when it is complete). How can I do this within onCLSuspend()? - i.e. I need to start the web worker operation (this is no problem), but then I need to wait for it to call my callback function before continuing - what code can I put in to to do this?

    4)
    When I get the suspend notification, I may be in the middle of uploading data to SkyDrive,  or may be about to do so.  This may well take more than 5 seconds.  What happens if my operations aren’t complete within 5 seconds – will the OS simply stop the application at an arbritrary point?  If so, what should I do?

    Any suggestions much appreciated.

    Regards

    Geoff Olding

    Tuesday, October 2, 2012 7:26 AM

All replies

  • Hope these answers can help a bit:

    1. The best way to run an async operation in checkpoint/suspend is to use the setPromise(), there's an example of how this is used in the activated handler in most of the javascript app templates. Basically you do the following:

    WinJS.Application.oncheckpoint = function (args) {
        args.setPromise(someAsyncFunction());
    }

    2. If the checkpoint is scheduled for execution, it will run the next time your main thread yields such as when it's waiting for an async task to complete or becomes idle.

    3. Your web workers will automatically pause execution after finishing whatever synchronous block they're currently in and will resume when the app is resume. If the webworker is performing synchronous work and then calls back when it's finished, I don't see why that shouldn't just work in your case, but I haven't verified that.

    4. Yes, the OS will simply terminate the app if it fails to suspend in a certain time period. Some critical potentially long running task like this is a prime candidate for a background task that can continue execution after the app has been suspended. Background Tasks

    -Nick


    Nick Eubanks (MSFT)

    Tuesday, October 2, 2012 6:48 PM
  • Hi Nick

    Many thanks for your quick reply.

    I OK - I feel that it would be useful to have a specific example of using setPromise() in the "checkpoint" event in the documentation. Am I right in thinking that the application won't actually be suspended until the function "someAsyncFunction()" has completed (assuming that this is within the 5 second limit)?

    2 In this case, the notification to suspend may get called whilst the main thread is implementing a particular bit of functionality (if this functionality is asyncronous). For example, I might be in the middle of the asyncronous function "doSomeStuff()" when the onCheckPoint() function gets called.  I appreciate that onCheckPoint() won’t be called in the middle of a syncronous section of code in doSomeStuff(), however it can “nip” in whilst doSomeStuff() is waiting for an asyncronous operation to complete.  In this scenario, will someAsyncFunction() only be started once doSomeStuff() has completed?  (assuming that I am using setPromise()) If not, how do I write “someAsyncFunction()” so that it can “hang around and do nothing” until doSomeStuff() is complete?

    3 This is a similar point to 2), in that I want to "hang around and do nothing" until a particular function has been called - in this case the function that I call when I complete an asyncronous background task using a web worker.  Is this possible?  I am assuming that my web worker thread will continue to run until the application is actually suspended.

    5 I use a lot of timers in my application. Mainly, they are used to ensure that my operations are implemented sequentially. What happens to these timers during the suspension procedure? I.e. am I right in thinking that they will continue to run whilst someAsyncFunction() is running, but then stop when the application is suspended?

    6 Is it possible to systmetically test the activation of the application after a termination?  I use the “Suspend and Shutdown” in Visual Studio, however I am unclear how then to simulate a subsequent activation – i.e. when you run the application again from within Visual Studio, is it now simulating the activation of the application after a termination, or is it simulating the launch of the application from scratch as usual (is this in any case a distinction that we need to worry about?)  Is it possible to cause an application to be systemtically suspended or terminated when not running the application from within Visual Studio?

    Many thanks

    Geoff Olding

    Wednesday, October 3, 2012 4:05 PM