Triggers UWP尝试运行后台服务引发异常
我正在尝试在UWP应用程序中运行后台服务。我首先检查应用程序是否具有后台权限。如果是,则我正在注册服务以运行 在我将VisualStudio和Windows10SDK一起更新为Creators更新版本之前,这段代码一直运行良好。现在我不知道这个更新是否会改变注册后台服务的情况Triggers UWP尝试运行后台服务引发异常,triggers,uwp,background-service,Triggers,Uwp,Background Service,我正在尝试在UWP应用程序中运行后台服务。我首先检查应用程序是否具有后台权限。如果是,则我正在注册服务以运行 在我将VisualStudio和Windows10SDK一起更新为Creators更新版本之前,这段代码一直运行良好。现在我不知道这个更新是否会改变注册后台服务的情况 using System; using Windows.ApplicationModel.Background; using BackgroundService; using SampleApp.Config; name
using System;
using Windows.ApplicationModel.Background;
using BackgroundService;
using SampleApp.Config;
namespace SampleApp.Background
{
class BackgroundClass
{
LocalConfig LC = new LocalConfig();
public async void RequestBackgroundAccess()
{
var result = await BackgroundExecutionManager.RequestAccessAsync();
switch (result)
{
case BackgroundAccessStatus.AllowedMayUseActiveRealTimeConnectivity:
break;
case BackgroundAccessStatus.AllowedWithAlwaysOnRealTimeConnectivity:
break;
case BackgroundAccessStatus.Denied:
break;
case BackgroundAccessStatus.Unspecified:
break;
}
}
public async void RegisterBackgroundSync()
{
var trigger = new ApplicationTrigger();
var condition = new SystemCondition(SystemConditionType.InternetAvailable);
if (!LC.BackgroundSyncStatusGET())
{
var task = new BackgroundTaskBuilder
{
Name = nameof(BackgroundSync),
CancelOnConditionLoss = true,
TaskEntryPoint = typeof(BackgroundSync).ToString(),
};
task.SetTrigger(trigger);
task.AddCondition(condition);
task.Register();
LC.BackgroundSyncStatusSET(true);
}
await trigger.RequestAsync(); //EXCEPTION HAPPENS AT THIS LINE
}
public void RegisterBackgroundService(uint time)
{
var taskName = "BackgroundService";
foreach (var unregisterTask in BackgroundTaskRegistration.AllTasks)
{
if (unregisterTask.Value.Name == taskName)
{
unregisterTask.Value.Unregister(true);
}
}
if(time != 0)
{
var trigger = new TimeTrigger(time, false);
var condition = new SystemCondition(SystemConditionType.InternetAvailable);
var task = new BackgroundTaskBuilder
{
Name = nameof(BackgroundService),
CancelOnConditionLoss = true,
TaskEntryPoint = typeof(BackgroundService).ToString(),
};
task.SetTrigger(trigger);
task.AddCondition(condition);
task.Register();
}
}
}
}
现在,在请求时,我正在检查后台服务是否已注册,并保留重新注册的问题。我得到了以下例外
发生System.Runtime.InteropServices.COMException异常 HResult=0x80004005 Message=调用COM组件返回错误HRESULT E_FAIL 来源=Windows 堆栈跟踪: 在Windows.ApplicationModel.Background.ApplicationTrigger.RequestAsync()中 在SampleApp.Background.BackgroundClass.d_uu2.MoveNext()中
请帮助我的Windows 10隐私设置中也有同样的问题 系统设置=>隐私设置 在左侧菜单中选择
后台应用程序
检查以确保您的应用程序未被阻止运行后台任务。您发布的代码似乎正确。这里的问题可能不在这里。你试过了吗?它也有同样的问题吗?您还可以在其他设备上尝试使用代码。如果你仍然有同样的问题,我建议你分享一个,这样我们可以复制你的问题。