[英]Windows phone 8.1 background task memory management
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張圖片后消失。
有沒有辦法在這里處理內存? 請注意,我使用的是Background Transfer聯網api,它會自行完成所有文件分割。
不要等待上傳的結果。
您在BackgroundTask中要做的只是開始上傳。 此時,您已將其交給BackgroundTransfer進行管理。 BackgroundTransfer的目的是在應用程序外部處理您的上載,它將在您的應用程序暫停時上載,甚至可以在手機重啟后繼續。
在您的情況下,通過不等待就可以循環處理,而只是開始處理下一個文件。 如果要成功輸出消息,請使用Progress回調。
嗨,哥迪,我認為這是完成上傳的最佳選擇,因為如果您執行后台任務,則用戶可以刪除圖像或添加其他圖像,並且將來您可能會遇到問題。 然后,您可以使用進度條或其他程序來阻止屏幕顯示,以便用戶等待此過程完成,我認為更正確! 祝好運!
PD:很抱歉要添加答案,但我沒有信譽,不能添加評論,很抱歉和管理員!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.