我正在构建一个带后台任务的通用应用。注册任务时,我正在应用系统条件,取消条件丢失设置为true:
builder.AddCondition(new SystemCondition(SystemConditionType.UserNotPresent));
builder.CancelOnConditionLoss = true;
这样可行,但问题是当条件丢失触发应用程序崩溃时。如果我不使用取消conditioin丢失,即使“用户在场”,任务仍继续运行。对此问题的任何帮助或建议将不胜感激。
这是后台任务运行方法:
async public void Run(IBackgroundTaskInstance taskInstance)
{
Debug.WriteLine(taskInstance.Task.Name + " task is running.......");
//Get a deferral if we're doing async work.
BackgroundTaskDeferral deferral = taskInstance.GetDeferral();
try
{
taskInstance.Canceled += (s, e) =>
{
cancelRequest = true;
s.Task.Unregister(true);
Debug.WriteLine(s.Task.Name + " task has been cancelled.");
};
while (cancelRequest == false)
{
Debug.WriteLine("inside loop");
await Task.Delay(15000);
}
}
catch (Exception ex)
{
Debug.WriteLine("An Error Has Occurred: " + ex.Message);
}
deferral.Complete();
}
更新: 通过进一步调查,注意到在输出控制台中应用程序抛出此错误:WWAHost.exe已退出代码1(0x1)