在并行 ForEach 循环中异步从网络下载图像
您好,我想在并行循环 foreach 中异步从网络下载图像。
我有带有签名 IDictionary
的字典。 用户类有两个属性:
Uri ProfilePhoto
BitmapImage ProfilePhotoAsBitmapImage
我的目标是,如果 ProfilePhoto 为 null,则循环遍历字典我得到默认头像,而不是我想从网络异步下载图像。
private void GetProfilePhotosFromServer(IEnumerable<UserInfo> friends)
{
Parallel.ForEach(friends, f =>
{
//get defualt
if (f.ProfilePhoto == null)
f.ProfilePhotoAsBitmap = CreateDefaultAvatar(f.Sex);
//start downloading from web server asynchronly
//problem is that I don’t know how retrieve BitmapImage object from
//StartDownloading method or method ProcessImage, which is on the bottom
//of my question
var image = StartDownloadingImage(f.MediumProfilePhoto);
image.Freeze();
f.ProfilePhotoAsBitmap = image;
});
}
问题是我不知道如何从 StartDownloading 方法或 ProcessImage 方法检索 BitmapImage 对象,这是我问题的底部。
启动 Web 请求:
private void StartDownloadingImage(Uri imageUri)
{
_webRequest = WebRequest.Create(imageUri);
_webRequest.BeginGetResponse(this.ProcessImage, null);
//how retrieve result of ProcessImage method
}
Web 请求完成后,我调用此方法:
private void ProcessImage(IAsyncResult asyncResult)
{
var response = _webRequest.EndGetResponse(asyncResult);
using (var stream = response.GetResponseStream())
{
var buffer = new Byte[response.ContentLength];
int offset = 0, actuallyRead = 0;
do
{
actuallyRead = stream.Read(buffer, offset, buffer.Length - offset);
offset += actuallyRead;
}
while (actuallyRead > 0);
var image = new BitmapImage
{
CreateOptions = BitmapCreateOptions.None,
CacheOption = BitmapCacheOption.OnLoad
};
image.BeginInit();
image.StreamSource = new MemoryStream(buffer);
image.EndInit();
//problem
return image;
}
}
在 ProcessImage 方法中创建 BitmapImage 对象,如何将此对象传递给 GetProfilePhotosFromServer 方法中使用的属性 od User 对象?
上面的方法从 MemoryStream BitampImage 对象创建。
Hi I would like download images from web asychrounly in parallel loop foreach.
I have dictionary with signature IDictionary<string,user>
.
User class has two properties:
Uri ProfilePhoto
BitmapImage ProfilePhotoAsBitmapImage
My aim is, go through dictionary in loop if ProfilePhoto is null I get default avatar, it not I would like download image from web asynchronly.
private void GetProfilePhotosFromServer(IEnumerable<UserInfo> friends)
{
Parallel.ForEach(friends, f =>
{
//get defualt
if (f.ProfilePhoto == null)
f.ProfilePhotoAsBitmap = CreateDefaultAvatar(f.Sex);
//start downloading from web server asynchronly
//problem is that I don’t know how retrieve BitmapImage object from
//StartDownloading method or method ProcessImage, which is on the bottom
//of my question
var image = StartDownloadingImage(f.MediumProfilePhoto);
image.Freeze();
f.ProfilePhotoAsBitmap = image;
});
}
Problem is that I don’t know how retrieve BitmapImage object from StartDownloading method or method ProcessImage, which is on the bottom of my question.
Start web request:
private void StartDownloadingImage(Uri imageUri)
{
_webRequest = WebRequest.Create(imageUri);
_webRequest.BeginGetResponse(this.ProcessImage, null);
//how retrieve result of ProcessImage method
}
After web request is finished I call this method:
private void ProcessImage(IAsyncResult asyncResult)
{
var response = _webRequest.EndGetResponse(asyncResult);
using (var stream = response.GetResponseStream())
{
var buffer = new Byte[response.ContentLength];
int offset = 0, actuallyRead = 0;
do
{
actuallyRead = stream.Read(buffer, offset, buffer.Length - offset);
offset += actuallyRead;
}
while (actuallyRead > 0);
var image = new BitmapImage
{
CreateOptions = BitmapCreateOptions.None,
CacheOption = BitmapCacheOption.OnLoad
};
image.BeginInit();
image.StreamSource = new MemoryStream(buffer);
image.EndInit();
//problem
return image;
}
}
BitmapImage object is created in ProcessImage method how can I pass this object to the property od User object which is used in GetProfilePhotosFromServer method?
Method above create from MemoryStream BitampImage object.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要传入附加操作和 UserInfo 对象作为异步方法的回调。最简单的方法是创建一个包含它们的类并将其作为方法的异步状态传递。
You need to pass in the additional operations and the UserInfo object as a callback to the asynchronous method. The easiest way to do this is to create a class containing them and pass that as the asynchronous state of the method.