C# 无法注册后台任务
我有以下背景任务:C# 无法注册后台任务,c#,uwp,background,desktop-bridge,C#,Uwp,Background,Desktop Bridge,我有以下背景任务: namespace Background { public sealed class BackgroundTask : IBackgroundTask { private BackgroundTaskDeferral _deferral; public void Run(IBackgroundTaskInstance taskInstance) { //_deferral = taskI
namespace Background
{
public sealed class BackgroundTask : IBackgroundTask
{
private BackgroundTaskDeferral _deferral;
public void Run(IBackgroundTaskInstance taskInstance)
{
//_deferral = taskInstance.GetDeferral();
}
在一个名为Task.cs的文件中(文件名并不重要)
背景名称空间在我的解决方案中,它包含一个打包项目,其中包含一个UWP解决方案,该解决方案应启动Backgroundtask:
private async void LaunchBackground()
{
await BackgroundExecutionManager.RequestAccessAsync();
var exampleTaskName = "Background";
foreach (var taskA in BackgroundTaskRegistration.AllTasks)
{
if (taskA.Value.Name == exampleTaskName)
{
await new ApplicationTrigger().RequestAsync();
break;
}
}
var builder = new BackgroundTaskBuilder();
builder.Name = exampleTaskName;
builder.TaskEntryPoint = "Background.BackgroundTask";
BackgroundTaskRegistration task = builder.Register();
await new ApplicationTrigger().RequestAsync();
}
在UWP和包装项目中,清单中声明了以下内容:
<Extensions>
<Extension Category="windows.backgroundTasks" EntryPoint="Background.BackgroundTask">
<BackgroundTasks>
<Task Type="systemEvent" />
</BackgroundTasks>
</Extension>
</Extensions>
这是我不想要的,我得到了一个新的错误:
在:
wait new ApplicationTrigger().RequestAsync()代码>System.Runtime.InteropServices.COMException:“调用COM组件时返回错误HRESULT E_FAIL。”
尝试以下代码,我们应检查后台任务访问权限,然后注册后台任务:
var hasAccess = await BackgroundExecutionManager.RequestAccessAsync();
if (hasAccess == BackgroundAccessStatus.DeniedBySystemPolicy
|| hasAccess == BackgroundAccessStatus.DeniedByUser
|| hasAccess == BackgroundAccessStatus.Unspecified)
{
await new MessageDialog("ACCESS DENIED").ShowAsync();
return;
}
/////////////////////begin registering bg task
var task = new BackgroundTaskBuilder
{
Name = "Background",
TaskEntryPoint = typeof(Background.BackgroundTask).ToString()
};
//Trigger is necessary for registering bg tasks but Conditions are optional
//set a trigger for your bg task to run
//for ex. below Trigger will run when toast Notifications (cleared, user pressed an action button and so on)
ToastNotificationActionTrigger actiontrigger = new ToastNotificationActionTrigger();
task.SetTrigger(actiontrigger);
//Optional Conditions like Internet Connection
//var condition = new SystemCondition(SystemConditionType.InternetAvailable);
//task.AddCondition(condition);//set condition
BackgroundTaskRegistration registration = task.Register();//Register the task
在注册后台任务之前,我们还应该检查它是否已经在运行。
类似于下面的代码片段:
var isAlreadyRegistered = BackgroundTaskRegistration.AllTasks.Any(t => t.Value?.Name == "BackroundTask");
if (isAlreadyRegistered)
{
foreach (var tsk in BackgroundTaskRegistration.AllTasks)
{
if (tsk.Value.Name == "BackroundTask")
{
tsk.Value.Unregister(true);
break;
}
}
}
更多信息@mjwill这些是必要的吗?我只想自己启动这个任务,如果我启动它,它在任何条件下都应该工作。编辑:请参见后期编辑
var isAlreadyRegistered = BackgroundTaskRegistration.AllTasks.Any(t => t.Value?.Name == "BackroundTask");
if (isAlreadyRegistered)
{
foreach (var tsk in BackgroundTaskRegistration.AllTasks)
{
if (tsk.Value.Name == "BackroundTask")
{
tsk.Value.Unregister(true);
break;
}
}
}