【发布时间】:2018-02-27 16:25:21
【问题描述】:
我正在从网页中提取一个 JSON 字符串,其中包含一个对象列表。每个对象中有两个字符串,它们都包含指向图像的链接。我想在本地下载这张图片。
我目前正在使用每隔几秒触发一次的异步函数,并在 JSON 中查找以前不在列表中的任何新对象,获取图像链接,然后像这样下载它们:
using (WebClient webClient = new WebClient())
{
await webClient.DownloadFileTaskAsync(new Uri(object.AvatarImage), System.IO.Directory.GetCurrentDirectory() + "\\Elements" + "\\" + object.PostID.Replace(':', '-') + "_avatar.png");
}
using (WebClient webClient = new WebClient())
{
await webClient.DownloadFileTaskAsync(new Uri(object.Media), System.IO.Directory.GetCurrentDirectory() + "\\Elements" + "\\" + object.PostID.Replace(':', '-') + "_media.png");
}
这可以正常工作,直到我收到大量图像,此时我有时会收到错误:
进程无法访问文件“path”,因为它正被另一个进程使用。
我没有在其他任何地方操作文件,虽然图像路径确实绑定到图像控件,但如果我破坏绑定,我仍然会遇到同样的错误。
这可能是什么原因造成的?
【问题讨论】:
-
如果您正在加载 BitmapImages:stackoverflow.com/a/13265190/1136211
标签: c# wpf asynchronous webclient