locked
Background Task is not working - how to debug?

    Question

  • I took a look at http://social.msdn.microsoft.com/Forums/en-US/winappswithhtml5/thread/7e53336b-6f38-40ec-89c9-77c340867f6c and used similar code...

    I register the task like and it registers ok... (BackgroundUtils is code from the link above that I refactored into a seperate namespace)

            if (!BackgroundUtils.isMainTileUpdaterTaskRegistered()) {
                BackgroundUtils.registerMainTileUpdater();
            }

    I waited for over 15 minutes but nothing happened...  My entry/start page looks like this:

    (function () {
        "use strict";
        importScripts("//Microsoft.WinJS.0.6/js/base.js");
        importScripts("js/utils-notify.js");
        //Begin task
        var backgroundTaskInstance = Windows.UI.WebUI.WebUIBackgroundTaskInstance.current;
    
        NotifyUtils.showToast();
    
        backgroundTaskInstance.succeeded = true;
        close();
        
    })();
    
    

    It simply calls a toast notification - this is just for testing.  I know that NotifyUtils.showToast(); works because I ran it on page load and it pops up a toast message...

    Any idea what is wrong?  Most importantly, any tips on how do I debug a background task?  



    www.emadibrahim.com

    Thursday, May 24, 2012 11:30 AM

Answers

  • You should be able to debug your background task normally through visual studio.

    For example, I downloaded and ran the background task sample.  Ran it.  Put a BP at the top of backgroundtask.js, selected scenario 2 and regsitered the task, then changed my time zone and it broke just fine in the debugger.

    What are you doing in your case?

    -Jeff


    Jeff Sanders (MSFT)

    Thursday, May 24, 2012 5:43 PM
    Moderator
  • Look like my importScripts format was wrong so it was throwing an exception before getting to my breakpoint...  I wrapped in a try/catch and was able to figure out the problem...

        try {
            importScripts("//Microsoft.WinJS.0.6/js/base.js");
            importScripts("/js/utils-notify.js");
            
            //Begin task
            var backgroundTaskInstance = Windows.UI.WebUI.WebUIBackgroundTaskInstance.current;
    
            NotifyUtils.showToast("back ground worker triggered");
    
            backgroundTaskInstance.succeeded = true;
        } catch(e) {
            console.log(e);
        } 
        
        close();


    www.emadibrahim.com

    • Marked as answer by Emad Friday, May 25, 2012 2:11 AM
    Friday, May 25, 2012 2:10 AM

All replies

  • You should be able to debug your background task normally through visual studio.

    For example, I downloaded and ran the background task sample.  Ran it.  Put a BP at the top of backgroundtask.js, selected scenario 2 and regsitered the task, then changed my time zone and it broke just fine in the debugger.

    What are you doing in your case?

    -Jeff


    Jeff Sanders (MSFT)

    Thursday, May 24, 2012 5:43 PM
    Moderator
  • Look like my importScripts format was wrong so it was throwing an exception before getting to my breakpoint...  I wrapped in a try/catch and was able to figure out the problem...

        try {
            importScripts("//Microsoft.WinJS.0.6/js/base.js");
            importScripts("/js/utils-notify.js");
            
            //Begin task
            var backgroundTaskInstance = Windows.UI.WebUI.WebUIBackgroundTaskInstance.current;
    
            NotifyUtils.showToast("back ground worker triggered");
    
            backgroundTaskInstance.succeeded = true;
        } catch(e) {
            console.log(e);
        } 
        
        close();


    www.emadibrahim.com

    • Marked as answer by Emad Friday, May 25, 2012 2:11 AM
    Friday, May 25, 2012 2:10 AM
  • Jeff, I got the background task working using the time zone trigger but I can't get it to work with the timer trigger.  I used the same exact code for the time zone trigger and switched the trigger to a 15 minute timer, uninstalled the app, re-launched it and waited for 30 minutes - nothing happened!!!  

    What's wrong and how do I debug that?

    PS: I even let it run overnight, the tile was supposed to change but nothing happened  


    www.emadibrahim.com

    Friday, May 25, 2012 12:55 PM
  • Did you follow the documentation on background tasks? Introduction to Background Tasks

    As such, every app that is on the lock screen must implement a background task of either the Control Channel, Timer, or Push Notification type. These tasks will allow an app to always reflect accurate, up-to-date information in real-time. Going hand-in-hand with this, any app that needs this type of background task will need to be on the lock screen.

    The sample also shows how to do this:http://code.msdn.microsoft.com/windowsapps/Background-Task-Sample-9209ade9

    -Jeff


    Jeff Sanders (MSFT)

    Friday, May 25, 2012 1:47 PM
    Moderator
  • I missed that part in the document...  What if I don't want my app to be in the lock screen?  I just want to update my tile...

    Another problem, I am having now, is that I get the popup for the permission but it disappears very quickly before I can accept/decline access...  any idea?


    www.emadibrahim.com

    Friday, May 25, 2012 2:56 PM
  • I found a workaround...  I added a timer on my home screen to wait 5 seconds before displaying the prompt...  Seems to work fine...

    window.setTimeout(BackgroundUtils.requestLockScreenAccess, 5000);


    www.emadibrahim.com

    Friday, May 25, 2012 3:05 PM
  • Glad you have a workaround.  I believe that was a bug wish should be fixed in the next build if I remember correctly.

    What information are you displaying?  That will help me assist you in determining what mechanism is the best for your to use.

    -Jeff


    Jeff Sanders (MSFT)

    Friday, May 25, 2012 6:20 PM
    Moderator
  • I am displaying 5 tasks that are due today...  don't really care about lock screen, I just want tile updates

    www.emadibrahim.com

    Friday, May 25, 2012 6:32 PM
  • I see, and there is a possiblity that the app would not have been run 'today' correct?

    What if you schedule the tile update when the item is entered?  I assume the item is entered VIA your applicaiton so your app will be up and running (make sure you set an expiration time as well).

    You would use the scheduled tile notification:

    Notifications.ScheduledTileNotification


    Jeff Sanders (MSFT)

    Friday, May 25, 2012 6:50 PM
    Moderator
  • that wont work.  I want it to run every 15 minutes even if my app is not running...

    www.emadibrahim.com

    Friday, May 25, 2012 7:04 PM
  • Can't you schedule a notification for every 15 minutes (and expire it too)?

    If not, we need to know a lot more about what you are trying to do and why.  Finally you can use push notifications from a webservice.

    -Jeff


    Jeff Sanders (MSFT)

    Tuesday, May 29, 2012 7:41 PM
    Moderator
  • I got it working... Not sure what happened but a breakpoint triggered after a while...  

    www.emadibrahim.com

    Tuesday, May 29, 2012 8:07 PM