How to improve performance method GetThumbnailAsync in window phone 8.1
Asked Answered
I

1

2

I write a function to show images on a folder (assume i have about 60 images in this folder) in window phone 8.1. And the problem is function GetThumbnailAsync() take so long time when i create stream to get bitmapImage. Here's my code

       //getFileInPicture is function get all file in picture folder 
       List<StorageFile> lstPicture = await getFileInPicture();
       foreach (var file in lstPicture)
       {
            var thumbnail = await        file.GetThumbnailAsync(Windows.Storage.FileProperties.ThumbnailMode.PicturesView,50);
            var bitmapImage = new BitmapImage();
            bitmapImage.DecodePixelWidth = (int)(ScreenWidth / 4);
            await bitmapImage.SetSourceAsync(thumbnail);
            g_listBitmapImage.Add(bitmapImage);
           //g_listBitmapImage is a list of bitmapImage
        }

I was test and find the problem is function GetThumbnailAsync take so long time. If i have about 60 picture it take about 15second to finish this function( i test in lumia 730). Have anybody get this problem and how to make this code run faster ?.

Thanks so much for your supports

Inherent answered 30/5, 2015 at 14:2 Comment(0)
D
6

You are currently awaiting file.GetThumbnailAsync for each file which means that although the function is executed asynchronously for each file, it is executed in order, not in parallel.

Try converting each async operation returned from file.GetThumbnailAsyncto a Task and then storing those in a list and then await all tasks using Task.WhenAll.

List<StorageFile> lstPicture = await getFileInPicture();
List<Task<StorageItemThumbnail>> thumbnailOperations =  List<Task<StorageItemThumbnail>>();

   foreach (var file in lstPicture)
   {
        thumbnailOperations.Add(file.GetThumbnailAsync(Windows.Storage.FileProperties.ThumbnailMode.PicturesView,50).AsTask());
   }

   // wait for all operations in parallel
   await Task.WhenAll(thumbnailOperations);

   foreach (var task in thumbnailOperations)
   {
       var thumbnail = task.Result;
        var bitmapImage = new BitmapImage();
        bitmapImage.DecodePixelWidth = (int)(ScreenWidth / 4);
        await bitmapImage.SetSourceAsync(thumbnail);
        g_listBitmapImage.Add(bitmapImage);
       //g_listBitmapImage is a list of bitmapImage
   }
Dorothydorp answered 30/5, 2015 at 14:22 Comment(7)
Hello, thanks so much for your help, but this function thumbnailOperations.Add(file.GetThumbnailAsync(Windows.Storage.FileProperties.ThumbnailMode.PicturesView,50)) has a error. file.getThumbnailAsync() has a IAsyncOperation<StorageItemThumbnail> type, and make thumbnailOperations can't add it.Inherent
@PhamLuc It looks like the answerer made a typo: put .StartAsTask() in the inner part of the parentheses, right after .GetThumbnailAsync().Redhanded
@James Ko , sorry but file.GetThumbnailAsync() doen't have function .StartAsTask(), so can't put as what you say..Inherent
@PhamLuc Try deleting .StartAsTask(), putting a dot next to it, and seeing if there's any method in the autocompletion that indicates it can convert it to Task (i.e. .AsTask()). If that doesn't work, try and see if you can add a using statement to this class, because there's an extension method to convert IAsyncOperation to Task there.Redhanded
@James Ko, thanks so much. After add .AsTask(), the code can run on debug mode of visual studio 2013. It get faster than about 5second. But if i run without visual studio 2013, it crash. could you add my skype nick ? or give me your email, i will send my code for your review. my skype mail : [email protected]Inherent
I have fixed the typo, what error do you get when the code crashes?Dorothydorp
@NedStoyanov : You Sir have saved the day.Thank you so much!Meagher

© 2022 - 2024 — McMap. All rights reserved.