我有一个带有一些后台任务的UWP应用程序。当我在VS2017中执行任务时,他们确实工作,并且数据库中的结果是正确的。但是当我创建测试版并将其安装到商店上时,任务似乎永远不会被执行。
两个任务都有一个与此类似的运行方法:
public async void Run(IBackgroundTaskInstance taskInstance)
{
var deferral = taskInstance.GetDeferral();
// .. some logic
}
finally
{
Debug.WriteLine("ClearPayment started");
deferral.Complete();
}
}
然后我在扩展的Splash屏幕中注册它们:
private async void RegisterTasks()
{
var backgroundAccessStatus = await BackgroundExecutionManager.RequestAccessAsync();
if (backgroundAccessStatus == BackgroundAccessStatus.DeniedByUser)
{
await Mvx.Resolve<IDialogService>().ShowMessage(Strings.BackgroundAccessDeniedTitle,
Strings.BackgroundAccessDeniedByUserMessage);
}
else if (backgroundAccessStatus == BackgroundAccessStatus.DeniedByUser)
{
await Mvx.Resolve<IDialogService>().ShowMessage(Strings.BackgroundAccessDeniedTitle,
Strings.BackgroundAccessDeniedByPolicyMessage);
}
else
{
RegisterClearPaymentTask();
RegisterRecurringPaymentTask();
}
}
private void RegisterClearPaymentTask()
{
// Unregister existing task first.
if (BackgroundTaskRegistration.AllTasks.Any(task => task.Value.Name == CLEAR_PAYMENTS_TASK))
{
BackgroundTaskRegistration.AllTasks.First(task => task.Value.Name == CLEAR_PAYMENTS_TASK).Value.Unregister(true);
}
var builder = new BackgroundTaskBuilder
{
Name = CLEAR_PAYMENTS_TASK,
TaskEntryPoint = String.Format("{0}.{1}", TASK_NAMESPACE, CLEAR_PAYMENTS_TASK)
};
// Task will be executed all 30 minutes
builder.SetTrigger(new TimeTrigger(30, false));
builder.Register();
}
private void RegisterRecurringPaymentTask()
{
// Unregister existing task first.
if (BackgroundTaskRegistration.AllTasks.Any(task => task.Value.Name == RECURRING_PAYMENT_TASK))
{
BackgroundTaskRegistration.AllTasks.First(task => task.Value.Name == RECURRING_PAYMENT_TASK).Value.Unregister(true);
}
var builder = new BackgroundTaskBuilder
{
Name = RECURRING_PAYMENT_TASK,
TaskEntryPoint = String.Format("{0}.{1}", TASK_NAMESPACE, RECURRING_PAYMENT_TASK)
};
// Task will be executed all 30 minutes
builder.SetTrigger(new TimeTrigger(30, false));
builder.Register();
}
目前我在启动时取消注册每个任务并再次注册它们。我在某处读到这是推荐的过程,以确保在执行任务的情况下执行新逻辑。
和
这里没有例外,所以我认为入口点是正确的。 可能是他们不能正常工作的原因或我如何进一步调查?
URL GitHub存储库:https://github.com/MoneyFox/MoneyFox