C#Windows 8.1后台设备屏幕锁定时运行的任务

时间:2016-02-24 05:32:19

标签: c# win-universal-app

我在过去一周做了很多工作,为Windows通用项目创建了后台任务。我有一个很难,并且开始相信当设备屏幕被锁定时触发后台任务是不可能的。我使用SystemTriggerType.Useraway来触发后台任务。我会发布到目前为止我所得到的内容。任何帮助都会很棒!

这是我如何注册后台任务

    public static void RegisterTask()
    {
        try
        {
            var taskRegistered = false;
            var builder = new BackgroundTaskBuilder();

            foreach (var task in BackgroundTaskRegistration.AllTasks)
            {
                if (task.Value.Name == "ResponderBackgroundTask")
                {
                    Debug.WriteLine(task.Value.Name + " Task Already Registered!!");
                    taskRegistered = true;
                    break;
                }
            }

            if (!taskRegistered)
            {
                builder.Name = "ResponderBackgroundTask";
                builder.TaskEntryPoint = "BackgroundGps.BackgroundTask";
                builder.AddCondition(new SystemCondition(SystemConditionType.UserNotPresent));
                builder.AddCondition(new SystemCondition(SystemConditionType.InternetAvailable));
                builder.SetTrigger(new SystemTrigger(SystemTriggerType.UserAway, false));
                builder.CancelOnConditionLoss = true;
                var register = builder.Register();

                register.Completed += new BackgroundTaskCompletedEventHandler(OnComplete);
            }
        }
        catch (Exception ex)
        {
            Debug.WriteLine(ex.ToString());
            throw;
        }
    }

这是backgroundtask:

async public void Run(IBackgroundTaskInstance taskInstance)
{
    taskInstance.Canceled += new BackgroundTaskCanceledEventHandler(OnCanceled);

    Debug.WriteLine("Inside Run.......");

    BackgroundTaskDeferral deferral = taskInstance.GetDeferral();

    await Geolocate();

    deferral.Complete();
}

async static Task<HttpResponseMessage> Geolocate()
{
    Debug.WriteLine("Inside Async Geolocate");

    HttpResponseMessage response = new HttpResponseMessage();

    Geolocator geolocator = new Geolocator();
    geolocator.DesiredAccuracy = (PositionAccuracy) 20;
    geolocator.DesiredAccuracyInMeters = 30;

    var networkStatus = NetworkInformation.GetInternetConnectionProfile();
    bool status = true;

    while (status)
    {
        networkStatus = NetworkInformation.GetInternetConnectionProfile();

        Geoposition position = await geolocator.GetGeopositionAsync().AsTask();

        var latitude = position.Coordinate.Point.Position.Latitude;
        var longitude = position.Coordinate.Point.Position.Longitude;

        HttpClient client = new HttpClient();
        response = await client.GetAsync("http://www.mylocation.com/location?=latitude&longitude");
        Debug.WriteLine(response);

        if (networkStatus == null)
        {
            status = false;
        }

        if (cancelRequest == true)
        {
            return response;
        }

        await Task.Delay(15000);
    }

    return response; 
}

1 个答案:

答案 0 :(得分:0)

当我将UWP项目用于推送通知时,我在后台任务中遇到了一些问题。

UWP BackgroundTasks的主要问题是他们认为非常轻,耗费的CPU时间不多,否则操作系统会将其关闭。

我的问题是,我尝试使用非常繁重的服务来访问本地数据库,这需要占用CPU时间并被操作系统关闭。我的日志在一行中间被切断,导致我的记录器不能快速写入消息。

尝试在BackgroundTask中放入一些日志,以查看他是否通过触发器引发,并查找可能导致其被取消的繁重操作。

Web请求也可能是问题...