每天在UWP App中运行后台任务

时间:2015-11-20 20:18:36

标签: c# windows-10 uwp

我已经创建了一个带有后台任务的应用。现在,我每天或在系统启动后都在搜索运行此任务的选项。

有这样的选择吗?无法找到解决方案。

感谢。

2 个答案:

答案 0 :(得分:2)

如何使用BackgroundTask创建TimeTrigger

<强> 1。创建TimeTrigger

bool oneShot = false;
TimeTrigger hourlyTrigger = new TimeTrigger(60*24, oneShot );

<强> 2。创建BackgroundTask

string entryPoint = “Tasks.ExampleBackgroundTaskClass”;
string taskName   = “Example daily background task”;

BackgroundTaskRegistration task = RegisterBackgroundTask(entryPoint, taskName, hourlyTrigger, null);

来源:https://msdn.microsoft.com/en-us/library/windows/apps/mt186458.aspx

答案 1 :(得分:0)

试试这个助手

**public class BackgroundActionTask
{
    private const string Task_NAME = "BackgroundTasker";
    public async static void RunBacgroundTask(bool run)
    {
        if (run)
        {
            StorageHelper.SaveLocalSetting(BuiltSettings.BackroundTaskKey, Task_NAME);
        }
        else
        {
            if (StorageHelper.LocalSettings.Values.ContainsKey(BuiltSettings.BackroundTaskKey))
            {
                StorageHelper.LocalSettings.Values.Remove(BuiltSettings.BackroundTaskKey);
            }
        }
        if (StorageHelper.LocalSettings.Values.ContainsKey(BuiltSettings.BackroundTaskKey))
        {
            foreach (var task in BackgroundTaskRegistration.AllTasks)
            {
                task.Value.Unregister(true);
            }
            var access = await BackgroundExecutionManager.RequestAccessAsync();

            switch (access)
            {
                case BackgroundAccessStatus.Unspecified:
                    break;
                case BackgroundAccessStatus.AllowedMayUseActiveRealTimeConnectivity:
                    break;
                case BackgroundAccessStatus.AllowedWithAlwaysOnRealTimeConnectivity:
                    break;
                case BackgroundAccessStatus.Denied:
                    break;
                default:
                    break;
            }
            try
            {
                var task = new BackgroundTaskBuilder
                {
                    Name = Task_NAME,
                    TaskEntryPoint = typeof(BackgroundTasker.BackgroundTaskUpdater).ToString()
                };

                var trigger = new TimeTrigger(12 * 60, false);
                task.SetTrigger(trigger);
                task.Register();
            }
            catch (Exception ex)
            {
                DialogHelper.DisplayMessageDebug(ex.Message);
            }
        }
        else
        {
            foreach (var task in BackgroundTaskRegistration.AllTasks)
            {
                task.Value.Unregister(true);
            }
        }
    }**

添加到组件库

public async void Run(IBackgroundTaskInstance taskInstance)
    {
        // Get a deferral, to prevent the task from closing prematurely
        // while asynchronous code is still running.
        BackgroundTaskDeferral deferral = taskInstance.GetDeferral();
        var successTask = await ExecuteBackgrounTaskAsync();
        // when all task is completed...
        if(successTask)
             deferral.Complete();
    }