Windows 8后台任务进度处理程序未激活

时间:2014-03-06 11:27:13

标签: c# windows-8 background-process

我在Win 8应用程序中编写了一个后台任务,我正在努力定期检查Feed中的新项目。该任务有效,如果应用程序运行,则成功触发它的完成处理程序。但是,我无法启动progress事件处理程序。我正在使用帮助程序类来注册事件/重新附加处理程序

    public static async void RegisterBackgroundTask(string taskName, string entryPoint, uint checkTime, BackgroundTaskCompletedEventHandler completionMethod, BackgroundTaskProgressEventHandler progressHandler = null)
        {
            var backgroundAccessStatus = await BackgroundExecutionManager.RequestAccessAsync();
            if (backgroundAccessStatus == BackgroundAccessStatus.AllowedMayUseActiveRealTimeConnectivity ||
                backgroundAccessStatus == BackgroundAccessStatus.AllowedWithAlwaysOnRealTimeConnectivity)
            {
                foreach (var task in BackgroundTaskRegistration.AllTasks)
                {
                    if (task.Value.Name == taskName)
                    {
                        // Re-Register progress completion event handler.
                        task.Value.Completed += completionMethod;
                        task.Value.Progress += progressHandler;

                        return; // The task is already registered, so no need to set again.
                    }
                }...

The progress code:

private async void OnBackgroundTaskProgress(IBackgroundTaskRegistration task, BackgroundTaskProgressEventArgs e)
        {

            await dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                {
                    uint percentage = e.Progress;

                    Debug.WriteLine("Background task progress: " + e.Progress);

                    SubscriptionManager manager = (SubscriptionManager)App.Current.Resources["subManager"];

                    var localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;

                    if ((bool)localSettings.Values["currentlyCheckingPodcast"])
                    {
                        if (podcastUpdateStatus.Visibility == Windows.UI.Xaml.Visibility.Collapsed)
                        {
                            podcastUpdateStatus.Visibility = Windows.UI.Xaml.Visibility.Visible;
                        }

                        podcastUpdateStausText.Text = (string)localSettings.Values["currentCheckName"] + " " + (int)localSettings.Values["checkNo"] + "/" + manager.Subscriptions.Count;
                    }

                    if (e.Progress == 100) { podcastUpdateStatus.Visibility = Windows.UI.Xaml.Visibility.Collapsed; }
                });
        }

我的注册码:

BackgroundTaskHelpers.RegisterBackgroundTask("TileUpdater", "NarrowCastBackgroundTasks.TileUpdater", checkTimeuint, OnBackgroundTaskComplete, OnBackgroundTaskProgress); 

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

我找出了缺少的内容:只有在后台任务的实例中设置progress属性时才会触发进度事件处理程序。