BackgroundAgents not firing up(PeriodicAgents more precisely)
I implemented Background Agents using [URL="http://msdn.microsoft.com/en-us/library/windowsphone/develop/hh202941(v=vs.105).aspx"]this[/URL] link. I register background agents in [I][B]Application_Closing[/B][/I] event inside app.xaml
[QUOTE]private void Application_Closing(object sender, ClosingEventArgs e)
{
AutoResetEvent autoResetEvent = new AutoResetEvent(false);
ThreadPool.QueueUserWorkItem((object obj) =>
{
StartResourceIntensiveAgent();
StartPeriodicAgent();
autoResetEvent.Set();
});
autoResetEvent.WaitOne();
}[/QUOTE]
and remove background agents on [I][B]Application_Launching [/B][/I]event
[QUOTE]private void Application_Launching(object sender, LaunchingEventArgs e)
{
ThreadPool.QueueUserWorkItem((object obj) =>
{
RemoveResourceIntensiveAgent();
RemovePeriodicAgent();
});
}[/QUOTE]
my implementation for start and end stubs
[QUOTE]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;
}
}[/QUOTE]
Similar thing goes for ResourceIntensiveAgent.
inside my ScheduledAgent class i put a debug statement.
[QUOTE]protected override void OnInvoke(ScheduledTask task)
{
Debug.WriteLine("Came in to schedule task at " + DateTime.Now.ToLocalTime());
NotifyComplete();
}[/QUOTE]
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
[QUOTE]#if(DEBUG_AGENT)
ScheduledActionService.LaunchForTest(Constants.periodicTaskName, TimeSpan.FromSeconds(10));
#endif[/QUOTE]
in [B][I]StartPeriodicAgent [/I][/B]method.
Tried lot of things with it but could not succeed. Came across [URL="http://social.msdn.microsoft.com/Forums/is/wpdevelop/thread/d0ee00a0-6874-44bb-9397-8c9a9a012869"]this[/URL] and [URL="http://social.msdn.microsoft.com/Forums/en-US/wpdevelop/thread/cda95c9a-23ae-4d22-8027-e99c4d5d88a0/"]this[/URL] but no good answer present out there.
Am i doing it wrong or [B]PeriodicAgents[/B] are not behaving as they should.
Regards
Re: BackgroundAgents not firing up(PeriodicAgents more precisely)
Still stuck in it. Tried with Release build too but still no success. Please someone help.:(
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 :)
Re: BackgroundAgents not firing up(PeriodicAgents more precisely)
[QUOTE=joaocardoso;911564]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 :)[/QUOTE]
Since it's working with [B]ScheduledActionService.LaunchForTest[/B] it's not a 256Mb device.
In my experience, if it's working with [B]LaunchForTest[/B] 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)
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.
Re: BackgroundAgents not firing up(PeriodicAgents more precisely)
And for your information guys i am testing it on windows phone 8 emulator and device.
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
[QUOTE]<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>[/QUOTE]
- 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 [URL="http://msdn.microsoft.com/en-us/library/windowsphone/develop/hh202962(v=vs.105).aspx"]here[/URL]
- 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 [URL="http://msdn.microsoft.com/en-us/library/windowsphone/develop/hh286408(v=vs.105).aspx"]this link[/URL] 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
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.
Re: BackgroundAgents not firing up(PeriodicAgents more precisely)
[QUOTE=vinayppatil;911610]I read somewhere microsoft offcially announced non working of resorceintensive agent but i did not came through any official source though.
Cheerio[/QUOTE]
I don't know if this could be considered "officially", but MSDN forum has a case about it:
[url]http://social.msdn.microsoft.com/Forums/uk/wpdevelop/thread/65b2c08d-44c0-4974-ba40-78b1fc933031[/url]
regards
pg
Re: BackgroundAgents not firing up(PeriodicAgents more precisely)
[QUOTE=wizard_hu_;911615]Uh, the first one is a really good point. It is soooo obvious, and it is soooo easy to overlook.[/QUOTE]
And that's exactly what happened. :P
Re: BackgroundAgents not firing up(PeriodicAgents more precisely)
[QUOTE=pavarang;911618]I don't know if this could be considered "officially", but MSDN forum has a case about it:
[url]http://social.msdn.microsoft.com/Forums/uk/wpdevelop/thread/65b2c08d-44c0-4974-ba40-78b1fc933031[/url][/QUOTE]
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
Re: BackgroundAgents not firing up(PeriodicAgents more precisely)
[QUOTE=Loukt;911572](Some devices need internet connexion for the background agent to work)[/QUOTE]
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