locked
How to implement a background task in C++

    Question

  • I try to implement a backgrouned task in C++ follow the sample:

    1. register the backgroundtask builder like this:

      auto builder = ref new BackgroundTaskBuilder();

        builder->Name = backgroundTaskName;
        builder->TaskEntryPoint = "BackgroundTask2.WinRTComponent";
     builder->SetTrigger(ref new SystemTrigger(SystemTriggerType::TimeZoneChange, false));

        BackgroundTaskRegistration^ task = builder->Register();
     task->Progress += ref new BackgroundTaskProgressEventHandler(this, &BlankPage::OnProgress);
        task->Completed += ref new BackgroundTaskCompletedEventHandler(this, &BlankPage::OnCompleted);
     Application::Current->Suspending += ref new SuspendingEventHandler(this, &BlankPage::App_Suspending);

    2. Add a Metro style winRT component dll to the solution to implement the backgroundtask:

    namespace BackgroundTask2
    {
     public ref class WinRTComponent sealed : public IBackgroundTask
        {
        public:
            WinRTComponent();
      virtual void Run(Windows::ApplicationModel::Background::IBackgroundTaskInstance^ taskInstance);
        };
    }

    but the applicaiton alway crash when timezonechange triggered, and there is an error like this in VS11:

    First-chance exception at 0x75E556C4 (KernelBase.dll) in backgroundTaskHost.exe: 0x40080201: WinRT originate error (parameters: 0x80040154, 0x00000008, 0x00FFEB04)

    If I make any mistake? thanks a lot.

    Wednesday, March 21, 2012 3:48 AM

Answers

  • Hello,

     

    I think you forget these thing:

    1. The DLL project should be included as a reference into your metro app.
    2. In the Package.appxmanifest->Declarations. When we add the Background Tasks, the executable value is Backgroundtaskhost.exe, and the Entry Point value is appNamespace.appClassName in your DLL project.

     

    I hope these information can help you to solve this problem.

     

    Best regards,

    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by chenyue_nova Monday, March 26, 2012 6:19 AM
    Thursday, March 22, 2012 4:25 PM

All replies

  • Hello,

    Did you put the background task into a DLL? If so, I think the &BlankPage::OnCompleted can not be accessed in task->Completed += ref new BackgroundTaskCompletedEventHandler(this, &BlankPage::OnCompleted); You can debug to check if it is null in your DLL.

    Also, you can follow the background sample code in SDK
    http://code.msdn.microsoft.com/windowsapps/Background-Task-Sample-9209ade9/

    Best regards,
    Jesse
     


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, March 22, 2012 2:53 AM
  • I implement BackgroudTask2 as a Metro style winRT DLL project. I also check there are such a BackgroundTask2.dll in the debug folder.

    I remarked this two lines, but still the background task does not run.

    //task->Progress += ref new BackgroundTaskProgressEventHandler(this, &BlankPage::OnProgress);
    //task->Completed += ref new BackgroundTaskCompletedEventHandler(this, &BlankPage::OnCompleted);

    Really, I cannot find any difference between sample codes and mine. Is there any step by step sample? thanks

    Thursday, March 22, 2012 5:58 AM
  • Hello,

     

    I think you forget these thing:

    1. The DLL project should be included as a reference into your metro app.
    2. In the Package.appxmanifest->Declarations. When we add the Background Tasks, the executable value is Backgroundtaskhost.exe, and the Entry Point value is appNamespace.appClassName in your DLL project.

     

    I hope these information can help you to solve this problem.

     

    Best regards,

    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by chenyue_nova Monday, March 26, 2012 6:19 AM
    Thursday, March 22, 2012 4:25 PM
  • Hello,

    I think you forget these thing:

    1. The DLL project should be included as a reference into your metro app.
    2. In the Package.appxmanifest->Declarations. When we add the Background Tasks, the executable value is Backgroundtaskhost.exe, and the Entry Point value is appNamespace.appClassName in your DLL project.

    I hope these information can help you to solve this problem.

    Best regards,

    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    Problem resolved, thanks a lot. I did not add a reference to my metro app.
    Monday, March 26, 2012 6:17 AM