×

Discussion Board

Results 1 to 12 of 12
  1. #1
    Regular Contributor
    Join Date
    Dec 2012
    Posts
    66

    BackgroundAgents not firing up(PeriodicAgents more precisely)

    I implemented Background Agents using this link. I register background agents in Application_Closing event inside app.xaml

    private void Application_Closing(object sender, ClosingEventArgs e)
    {
    AutoResetEvent autoResetEvent = new AutoResetEvent(false);
    ThreadPool.QueueUserWorkItem((object obj) =>
    {
    StartResourceIntensiveAgent();
    StartPeriodicAgent();
    autoResetEvent.Set();
    });
    autoResetEvent.WaitOne();
    }
    and remove background agents on Application_Launching event

    private void Application_Launching(object sender, LaunchingEventArgs e)
    {
    ThreadPool.QueueUserWorkItem((object obj) =>
    {
    RemoveResourceIntensiveAgent();
    RemovePeriodicAgent();
    });
    }
    my implementation for start and end stubs

    static PeriodicTask periodicTask;
    static ResourceIntensiveTask resourceIntensiveTask;

    public static PeriodicTask StartPeriodicAgent()
    {
    // Obtain a reference to the period task, if one exists
    periodicTask = ScheduledActionService.Find(Constants.periodicTaskName) as PeriodicTask;

    // If the task already exists and background agents are enabled for the
    // application, remove the task and then add it again to update
    // the schedule
    if (periodicTask != null)
    {
    RemovePeriodicAgent();
    }

    periodicTask = new PeriodicTask(Constants.periodicTaskName);

    // The description is required for periodic agents. This is the string that the user
    // will see in the background services Settings page on the device.
    periodicTask.Description = "a periodic task.";

    // Place the call to Add in a try block in case the user has disabled agents.
    try
    {
    ScheduledActionService.Add(periodicTask);
    }
    catch (Exception exception)
    {
    periodicTask = null;
    throw exception;
    }
    return periodicTask;
    }

    public static void RemovePeriodicAgent()
    {
    try
    {
    periodicTask = ScheduledActionService.Find(Constants.periodicTaskName) as PeriodicTask;

    if (periodicTask != null)
    {
    ScheduledActionService.Remove(Constants.periodicTaskName);
    periodicTask = null;
    }
    }
    catch (Exception e)
    {
    throw e;
    }
    }
    Similar thing goes for ResourceIntensiveAgent.
    inside my ScheduledAgent class i put a debug statement.

    protected override void OnInvoke(ScheduledTask task)
    {
    Debug.WriteLine("Came in to schedule task at " + DateTime.Now.ToLocalTime());
    NotifyComplete();
    }
    Now i run the application in debug mode and exit it, i can see my apps name in settings-> background tasks with "allowed" marked on it in red.
    I leave this debbuging state for 2 hours and when i come back i can not see anything in the debug log. But the same thing works when i put

    #if(DEBUG_AGENT)
    ScheduledActionService.LaunchForTest(Constants.periodicTaskName, TimeSpan.FromSeconds(10));
    #endif
    in StartPeriodicAgent method.


    Tried lot of things with it but could not succeed. Came across this and this but no good answer present out there.
    Am i doing it wrong or PeriodicAgents are not behaving as they should.

    Regards

  2. #2
    Regular Contributor
    Join Date
    Dec 2012
    Posts
    66

    Re: BackgroundAgents not firing up(PeriodicAgents more precisely)

    Still stuck in it. Tried with Release build too but still no success. Please someone help.

  3. #3
    Nokia Developer Moderator
    Join Date
    Feb 2011
    Location
    Portugal
    Posts
    940

    Re: BackgroundAgents not firing up(PeriodicAgents more precisely)

    Out of curiosity are you using the 256mb emulator? That version does not support background tasks.

    Tip: when asking questions if possible include the emulator that you are using or the physical device. We now have Windows Phone 7.1, 7.8, 8. On 7.1 and 7.8 you have also 256bm and 512mb devices. So its always a good thing to give it some context

  4. #4
    Nokia Developer Champion
    Join Date
    Sep 2012
    Location
    Morocco
    Posts
    295

    Re: BackgroundAgents not firing up(PeriodicAgents more precisely)

    Quote Originally Posted by joaocardoso View Post
    Out of curiosity are you using the 256mb emulator? That version does not support background tasks.

    Tip: when asking questions if possible include the emulator that you are using or the physical device. We now have Windows Phone 7.1, 7.8, 8. On 7.1 and 7.8 you have also 256bm and 512mb devices. So its always a good thing to give it some context
    Since it's working with ScheduledActionService.LaunchForTest it's not a 256Mb device.

    In my experience, if it's working with LaunchForTest method in debug mode, just try it on your device in release mode, it should work.
    (Some devices need internet connexion for the background agent to work)

  5. #5
    Regular Contributor
    Join Date
    Dec 2012
    Posts
    66

    Re: BackgroundAgents not firing up(PeriodicAgents more precisely)

    @joaocardoso: No its not a 256MB emulator. I am using 512MB emulator. I tested it on HTS Windows Phone 8S which too has 512MB of RAM.

    @Loukt : As i mentioned i tried it with release mode too. Do not works.

    Still on the same lines as i were yesterday. Almost did whatever i can but no success yet. Will take a break and try out again. If you guys have some links with official Microsoft engineers can you please confirm with this issue.

    Regards.

  6. #6
    Regular Contributor
    Join Date
    Dec 2012
    Posts
    66

    Re: BackgroundAgents not firing up(PeriodicAgents more precisely)

    And for your information guys i am testing it on windows phone 8 emulator and device.

  7. #7
    Regular Contributor
    Join Date
    Dec 2012
    Posts
    66

    Re: BackgroundAgents not firing up(PeriodicAgents more precisely)

    Okay, after 2 days of trying finally got it working. What i was doing wrong was debugging and ExtendedTask was missing from my WMAppManifest. :| But keep these few things in mind while working on Background Agents.

    - Debug statements do not work when you are in release mode.

    - Putting Debug.Writeline() inside a BackgroundAgent is not a good way of testing. It works only when you invoke background agent using ScheduledActionService.LaunchForTest() but not when OS has scheduled it.

    - Windows phone 8 puts strict constraints on memory and time limitations for background agent even while debugging (not sure about time though). But windows phone 7 does not. So if possible debug your background agents on Windows Phone 7 as debugging requires lot of space and time.

    - Even if you add a SheduleAgent by "New -> Windows Phone Schedule Task Agent" make sure an "ExtendedTask" is added to your main projects WMAppManifest.xml. If not add it in WMAppManifest.xml yourself using below format
    <Tasks>
    <DefaultTask Name="_default" NavigationPage="MainPage.xaml" />
    <ExtendedTask Name="BackgroundTask">
    <BackgroundServiceAgent Specifier="ScheduledTaskAgent" Name="Task_Name" Source="Assembly_Name" Type="Assembly_Name.ScheduledAgent" />
    </ExtendedTask>
    </Tasks>
    - There are API's which are not allowed to be used inside a background agent. Make sure you are not using any of them. Go throught the list of unsupported API's here

    - Best way to debug a background agent is to write to your isolated storage. And you can read your isolated storage contents using IsolatedStorageAccess tool (only for windows phone 8 though). Look at this link for details about this tool.

    - You can also put a Toast inside you background agent. But since toast automatically disappears after few seconds you may not notice it. So writing to isolated storage is the best solution.

    - Above steps make sure PeriodicAgent will run every 30 mins but i have not yet seen ResourceIntensiveTask running till now even after meeting all the requirements mentioned in documentation (External power, wifi etc). I read somewhere microsoft offcially announced non working of resorceintensive agent but i did not came through any official source though.

    Cheerio

  8. #8
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,689

    Re: BackgroundAgents not firing up(PeriodicAgents more precisely)

    Uh, the first one is a really good point. It is soooo obvious, and it is soooo easy to overlook.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Location
    Italy
    Posts
    639

    Re: BackgroundAgents not firing up(PeriodicAgents more precisely)

    Quote Originally Posted by vinayppatil View Post
    I read somewhere microsoft offcially announced non working of resorceintensive agent but i did not came through any official source though.

    Cheerio
    I don't know if this could be considered "officially", but MSDN forum has a case about it:
    http://social.msdn.microsoft.com/For...0-78b1fc933031

    regards
    pg

  10. #10
    Regular Contributor
    Join Date
    Dec 2012
    Posts
    66

    Re: BackgroundAgents not firing up(PeriodicAgents more precisely)

    Quote Originally Posted by wizard_hu_ View Post
    Uh, the first one is a really good point. It is soooo obvious, and it is soooo easy to overlook.
    And that's exactly what happened. :P

  11. #11
    Regular Contributor
    Join Date
    Dec 2012
    Posts
    66

    Re: BackgroundAgents not firing up(PeriodicAgents more precisely)

    Quote Originally Posted by pavarang View Post
    I don't know if this could be considered "officially", but MSDN forum has a case about it:
    http://social.msdn.microsoft.com/For...0-78b1fc933031
    I cant see any official guy posting there. Also the guy claiming of Microsoft confirming the bug did not post any link to support incident. Well i don't know if we take it as official. But what i can assure is ResourceIntensiveTask isn't working.:P

    Regards

  12. #12
    Regular Contributor
    Join Date
    Dec 2012
    Posts
    66

    Re: BackgroundAgents not firing up(PeriodicAgents more precisely)

    Quote Originally Posted by Loukt View Post
    (Some devices need internet connexion for the background agent to work)
    Its true for HTC Windows Phone 8S. Periodic agent wrote to log every 28 mins until i was connected to network. I disconnected the network before sleeping and it did not fire the whole night and fired up again when i connected back in the morning. Will confirm it soon for Nokia Lumia 820 too.

    Regards

Similar Threads

  1. N8: CustomItem pointerDragged event not firing
    By shockreaction in forum Mobile Java General
    Replies: 0
    Last Post: 2010-12-21, 12:21
  2. Firing alert from Application at a specific Time of Day
    By samiather in forum Mobile Java General
    Replies: 1
    Last Post: 2010-08-02, 06:02
  3. event firing
    By Arunesh in forum Mobile Java General
    Replies: 7
    Last Post: 2010-04-12, 11:17

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×