问题描述
我在窗口电话8.1中编写了一个功能来显示文件夹上的图像(假设我在此文件夹中有 60张图像)。当我创建流来获取bitmapImage时,问题是函数GetThumbnailAsync()需要很长时间。
这是我的代码
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
}
我是测试的发现问题是函数GetThumbnailAsync需要这么长时间。
如果我有大约60张图片,则需要大约15秒来完成此功能(我在lumia 730中测试)。
是否有人遇到此问题以及如何让此代码运行得更快? 。
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
推荐答案
您目前正在等待 file.GetThumbnailAsync
for每个文件意味着虽然函数是为每个文件异步执行的,但它是按顺序执行的,而不是并行执行的。
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.
尝试将从 file.GetThumbnailAsync
返回的每个异步操作转换为任务
然后将它们存储在列表中,然后使用 Task.WhenAll
等待
所有任务。
Try converting each async operation returned from file.GetThumbnailAsync
to 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
}
这篇关于如何在窗口电话8.1中改进性能方法GetThumbnailAsync的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!