【问题标题】:Windows phone 8.1 background task memory managementWindows phone 8.1 后台任务内存管理
【发布时间】:2014-10-03 21:55:05
【问题描述】:

windows phone 8.1 中的后台任务有 40mb 的有限内存。对于媒体上传到服务器或相关任务,40mb 相当少。

例如:我从 TimerTriggerTask 的 RUN 方法调用下面的函数。

    private async void AccessMediaUpload()
    {
        try
        {
            // SavedPictures can be used for working with emulators
            StorageFolder picfolder = KnownFolders.CameraRoll;
            var x = await picfolder.GetFilesAsync();

            var enumerator = x.GetEnumerator();

            Debug.WriteLine("Number of files are: " + x.Count);

            while (enumerator.MoveNext())
            {
                var file = enumerator.Current;

                // Setup the the uploader with the name of the file
                var uploader = new BackgroundUploader();
                uploader.SetRequestHeader("Filename", file.Name);

                // Start the upload
                UploadOperation upload = uploader.CreateUpload(uri, file);
                await upload.StartAsync();

                // Get the HTTP response to see the upload result
                ResponseInformation response = upload.GetResponseInformation();
                if (response.StatusCode == 200)
                {
                    Debug.WriteLine(file.Name + " Uplaoded");
                }
                //Debug.WriteLine("HTTP Status Code:" + response.StatusCode);
            }
            _deferral.Complete();
        }

        catch (OutOfMemoryException e)
        {
            Debug.WriteLine(e.Message);
        }
    }

如果我要上传大约 10 张图片,由于 OutOfMemoryException,它会在 4 张图片后消失。

有没有办法在这里处理内存?请注意,我使用的是后台传输网络 API,它自己完成所有文件分块。

【问题讨论】:

  • 您是否尝试过分析您的后台任务以查看是什么占用了这么多内存?
  • 我确实使用了 Windows phone 电动工具。它超过 40Mb,这就是后台任务死亡的时候。

标签: c# windows-phone windows-phone-8.1 background-task


【解决方案1】:

不要等待上传结果。

您想要在 BackgroundTask 中做的就是开始上传。此时,您已将其交给 BackgroundTransfer 为您管理。 BackgroundTransfer 的重点是处理你在应用外的上传,当你的应用挂起时它会上传,甚至在手机重启后也可以继续上传。

在您的情况下,循环处理,通过不等待您将开始处理下一个文件。如果要在成功时输出消息,请使用 Progress 回调。

【讨论】:

    【解决方案2】:

    嗨,golldy,我认为最好等待完成上传,因为如果您执行后台任务,用户可以删除图像或添加其他图像,您将来可能会遇到问题。然后,您可以使用进度条或相同的进度条阻止屏幕,让用户等待此过程完成,我认为这更正确!祝你好运!

    PD:抱歉添加答案,但我没有添加评论的声誉,srry golldy 和管理员!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多