I want to create a scheduled task agent for my windows phone application. I followed this link. But it seemed that the backgroud task exited wrongly. Here are some details:
- I have two projects in my solution:
TestApp (Windows Phone Silverlight 8.1)
andScheduledTaskAgent1 (Windows Phone Silverlight 8.1)
. - I have added the
ScheduledTaskAgent1
as a reference ofTestApp
. I have added the code below to
WMAppManifest.xml
inTestApp
:<Tasks> <DefaultTask Name ="_default" NavigationPage="MainPage.xaml" ActivationPolicy="Resume"/> <ExtendedTask Name="BackgroundTask"> <BackgroundServiceAgent Specifier="ScheduledTaskAgent" Name="ScheduledTaskAgent1" Source="ScheduledTaskAgent1" Type="ScheduledTaskAgent1.ScheduledAgent" /> </ExtendedTask> </Tasks>
In my
MainPage.xaml.cs
inTestApp
, I have the following code:private async void StartPeriodicTask() { periodicTask = ScheduledActionService.Find(periodicTaskName) as PeriodicTask; if (periodicTask != null) { try { ScheduledActionService.Remove(periodicTaskName); } catch (System.Exception) { } } periodicTask = new PeriodicTask(periodicTaskName); periodicTask.Description = "Lock Screen Image Changing Task"; periodicTask.ExpirationTime = DateTime.Now.AddDays(14); try { ScheduledActionService.Add(periodicTask); System.Diagnostics.Debug.WriteLine("Periodic task added."); #if(DEBUG_AGENT) System.Diagnostics.Debug.WriteLine("Starting debugging"); ScheduledActionService.LaunchForTest(periodicTaskName, TimeSpan.FromSeconds(15)); System.Diagnostics.Debug.WriteLine("Periodic task is started: " + periodicTaskName); #endif } catch (InvalidOperationException e) { if (e.Message.Contains("BNS Error: The action is disabled")) { MessageBox.Show("Background agents for this application have been disabled by the user."); } } catch (SchedulerServiceException) { System.Diagnostics.Debug.WriteLine("Periodic task added failed."); } } /* void StartPeriodicTask() */
In my
ScheduledAgent.cs
inScheduledTaskAgent1
, I have the following simple code:protected override void OnInvoke(ScheduledTask task) { System.Diagnostics.Debug.WriteLine("Background task accomplished."); NotifyComplete(); } /* OnInvoke() */
Other code in
ScheduledAgent.cs
kept unchanged as it was created automatcally by VS2013.After I started the program and called
StartPeriodicTask()
, I got these outputs:'BACKGROUNDTASKHOST.EXE' (CoreCLR: DefaultDomain): Loaded 'C:\windows\system32\mscorlib.ni.dll'. Cannot find or open the PDB file. 'BACKGROUNDTASKHOST.EXE' (CoreCLR: Silverlight AppDomain): Loaded 'C:\windows\system32\System.Windows.RuntimeHost.ni.dll'. Cannot find or open the PDB file. 'BACKGROUNDTASKHOST.EXE' (CoreCLR: Silverlight AppDomain): Loaded 'C:\windows\system32\System.Windows.ni.dll'. Cannot find or open the PDB file. 'BACKGROUNDTASKHOST.EXE' (CoreCLR: Silverlight AppDomain): Loaded 'C:\windows\system32\System.Net.ni.dll'. Cannot find or open the PDB file. 'BACKGROUNDTASKHOST.EXE' (CoreCLR: Silverlight AppDomain): Loaded 'C:\windows\system32\System.ni.dll'. Cannot find or open the PDB file. 'BACKGROUNDTASKHOST.EXE' (CoreCLR: Silverlight AppDomain): Loaded 'C:\windows\system32\System.Xml.ni.dll'. Cannot find or open the PDB file. 'BACKGROUNDTASKHOST.EXE' (CoreCLR: Silverlight AppDomain): Loaded 'C:\Data\Programs\{8BF294CB-9B46-4BD1-84A7-58C60582A35C}\Install\TestApp.DLL'. Symbols loaded. 'BACKGROUNDTASKHOST.EXE' (CoreCLR: Silverlight AppDomain): Loaded 'C:\windows\system32\Microsoft.Phone.ni.dll'. Cannot find or open the PDB file. 'BACKGROUNDTASKHOST.EXE' (CoreCLR: Silverlight AppDomain): Loaded 'C:\windows\system32\Microsoft.Phone.Interop.ni.dll'. Cannot find or open the PDB file. 'BACKGROUNDTASKHOST.EXE' (CoreCLR: Silverlight AppDomain): Loaded 'C:\Data\Programs\{8BF294CB-9B46-4BD1-84A7-58C60582A35C}\Install\ScheduledTaskAgent1.DLL'. Symbols loaded. Background task accomplished. The program '[0xCF0] BACKGROUNDTASKHOST.EXE' has exited with code 1 (0x1).
The last two lines showed the
OnInvoke()
method was called but the task exited wrongly.
So, how can I make the background task exits correctly?
Any reply will be appreciated.