Background task is terminated next time app launch

2019-07-02 15:47发布

I am playing with the Background Task code sample (c#) from MSDN. I modified the code a little bit to make it run longer, so that I can see if the application can get "re-attached" to the background task next time it is launched. My test shows that the background task always gets terminated when the application is launched for a second time. Here is the test detail:

The background task is trigger by a SystemTrigger of type SystemTriggerType.TimeZoneChange, and it works as expected if:

  1. I launch the application first and then change my TimeZone setting;
  2. I change my TimeZone first and then launch the application;

But if I close the application (by ALT+F4) and then launch it again before the background task completes. I will ALWAYS see that the background task is terminated. Sometimes I see messages like "The program '[920] backgroundTaskHost.exe: Managed (v4.0.30319)' has exited with code 1 (0x1)." in the Output window.

Why is the background task terminated every time the application is launched again? Is it the design? Or am I missing something here?

2条回答
一纸荒年 Trace。
2楼-- · 2019-07-02 16:10

That is the project reference problem.

If you exited program with code 1 (0x1) when you run backgrond task, you should add the background task project into the referece of original project.

[Windows 8] The crash problem about BackgroundTask shows you how to resolve the reference.

查看更多
SAY GOODBYE
3楼-- · 2019-07-02 16:30

If you are creating the BackgroundTask in C#, and using the Windows 8 RP, I think you may have to set the output type of the project to WinMD file and not class library. Are you using WinMD as the output type?

查看更多
登录 后发表回答