window phone 8.1 中如何提高性能方法 GetThumbnailAsync
How to improve performance method GetThumbnailAsync in window phone 8.1
我在 window phone 8.1 中编写了一个函数来显示文件夹中的图像(假设我在该文件夹中有大约 60 张图像)。问题是 函数 GetThumbnailAsync() 需要很长时间 当我创建流以获取位图图像时。
这是我的代码
//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 中测试)。
有人遇到过这个问题吗?如何使这段代码 运行 更快?.
非常感谢您的支持
您当前正在为每个文件等待 file.GetThumbnailAsync
,这意味着尽管该函数对每个文件异步执行,但它是按顺序执行的,而不是并行执行的。
尝试将从 file.GetThumbnailAsync
返回的每个异步操作转换为 Task
,然后将它们存储在列表中,然后 await
所有任务都使用 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
}
我在 window phone 8.1 中编写了一个函数来显示文件夹中的图像(假设我在该文件夹中有大约 60 张图像)。问题是 函数 GetThumbnailAsync() 需要很长时间 当我创建流以获取位图图像时。 这是我的代码
//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 中测试)。 有人遇到过这个问题吗?如何使这段代码 运行 更快?.
非常感谢您的支持
您当前正在为每个文件等待 file.GetThumbnailAsync
,这意味着尽管该函数对每个文件异步执行,但它是按顺序执行的,而不是并行执行的。
尝试将从 file.GetThumbnailAsync
返回的每个异步操作转换为 Task
,然后将它们存储在列表中,然后 await
所有任务都使用 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
}