How to improve performance method GetThumbnailAsync in window phone 8.1

ⅰ亾dé卋堺 提交于 2019-12-01 12:16:05

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
   }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!