0

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:

  1. I have two projects in my solution: TestApp (Windows Phone Silverlight 8.1) and ScheduledTaskAgent1 (Windows Phone Silverlight 8.1).
  2. I have added the ScheduledTaskAgent1 as a reference of TestApp.
  3. I have added the code below to WMAppManifest.xml in TestApp:

    <Tasks>
      <DefaultTask Name ="_default" NavigationPage="MainPage.xaml" ActivationPolicy="Resume"/>
      <ExtendedTask Name="BackgroundTask">
        <BackgroundServiceAgent Specifier="ScheduledTaskAgent" Name="ScheduledTaskAgent1" Source="ScheduledTaskAgent1" Type="ScheduledTaskAgent1.ScheduledAgent" />
      </ExtendedTask>
    </Tasks>
    
  4. In my MainPage.xaml.cs in TestApp, 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() */
    
  5. In my ScheduledAgent.cs in ScheduledTaskAgent1, 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.

  6. 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.

zsrkmyn
  • 547
  • 1
  • 5
  • 20
  • Have you tried this: http://stackoverflow.com/questions/25942340/backgroundtaskhost-exe-has-exited-with-code-1-0x1-geofence-issue or this http://stackoverflow.com/questions/24988200/backgroundtaskhost-exe-exits-with-code-1-0x1 – Igor Ralic Mar 09 '15 at 16:40
  • @igrali, sure! Of course I have read it. And he setted a background task in another way, but if I use `Windows Runtime Component` to build a background task, I cannot using LockScreenManger and some other lock screen api. – zsrkmyn Mar 09 '15 at 17:13
  • Any Solution on above issue. I am struggling for the same. If anyone have answer please update. – Leo Jun 30 '15 at 08:00
  • Had the same issue. Follow this link on how to add a project reference https://msdn.microsoft.com/en-us/library/hh708954.aspx Your Runtime Component must be listed under "References" of your Windows Phone Project – Ascot Harris May 11 '16 at 15:01

0 Answers0