Я работаю над BackgroundTask на UWP и пытаюсь запустить эту задачу, но по какой-то неизвестной причине она всегда заканчивается отображением приостановки в диспетчере задач и не запускается. Ниже приведен код
Вот как я регистрирую свою BackgroundTask
public static async Task InitializeLatestNewsTaskAsync()
{
TimeTrigger hourlyTrigger = new TimeTrigger(15, false);
BackgroundExecutionManager.RemoveAccess();
var requestStatus = await BackgroundExecutionManager
.RequestAccessKindAsync(BackgroundAccessRequestKind.AlwaysAllowed, "Please allow the app to run in background");
if (!requestStatus)
{
Util.MessageBox("Warrning", "You will not receive any Announcements notifications");
}
else
{
try
{
BackgroundTaskRegistration backgroundTaskRegistration = RegisterBackgroundTask(Constants.BACKGROUND_TASK_LATEST_NEWS_ENTRY_POINT,
Constants.BACKGROUND_TASK_LATEST_NEWS_NAME, hourlyTrigger, null);
}
catch (Exception e)
{
log.Error(e.Message, e);
}
}
}
//
// Register a background task with the specified taskEntryPoint, name, trigger,
// and condition (optional).
//
// taskEntryPoint: Task entry point for the background task.
// taskName: A name for the background task.
// trigger: The trigger for the background task.
// condition: Optional parameter. A conditional event that must be true for the task to fire.
//
private static BackgroundTaskRegistration RegisterBackgroundTask(
string taskEntryPoint, string taskName, IBackgroundTrigger trigger, IBackgroundCondition condition)
{
//
// Check for existing registrations of this background task.
//
foreach (var cur in BackgroundTaskRegistration.AllTasks)
{
if (cur.Value.Name == taskName)
{
//
// The task is already registered.
//
BackgroundTaskRegistration backgroundTaskRegistration = (BackgroundTaskRegistration)(cur.Value);
backgroundTaskRegistration.Unregister(true);
break;
}
}
//
// Register the background task.
//
var builder = new BackgroundTaskBuilder();
builder.Name = taskName;
builder.TaskEntryPoint = taskEntryPoint;
builder.SetTrigger(trigger);
builder.IsNetworkRequested = true;
if (condition != null)
{
builder.AddCondition(condition);
}
BackgroundTaskRegistration task = builder.Register();
task.Completed += new BackgroundTaskCompletedEventHandler(OnCompleted);
return task;
}
private static async void OnCompleted(IBackgroundTaskRegistration task, BackgroundTaskCompletedEventArgs args)
{
log.Info("Completed");
}
И вот мои объявления Package.appxmanifest
и реализация My BackgroundTask
public sealed class LatestNewsBackgroundTask : IBackgroundTask
{
private ILogger log = LogManagerFactory.DefaultLogManager.GetLogger<LatestNewsBackgroundTask>();
BackgroundTaskDeferral _deferral;
public void Run(IBackgroundTaskInstance taskInstance)
{
log.Info("Latest New Background Task Started");
_deferral = taskInstance.GetDeferral();
taskInstance.Canceled += new BackgroundTaskCanceledEventHandler(OnCanceled);
var localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
var announcementTypes = localSettings.Values["announcementTypes"];
NotificationHelper.GenerateToast("working from background task ", announcementTypes.ToString());
log.Info("Latest New Background Task Completed");
_deferral.Complete();
}
private void OnCanceled(IBackgroundTaskInstance sender, BackgroundTaskCancellationReason reason)
{
// Indicate that the background task is canceled.
log.Info("Background " + sender.Task.Name + " Cancel Requested...");
}
}
Все, что я пытался отладить
https://docs.microsoft.com/en-us/windows/uwp/launch-resume/debug-a-background-task