I am pulling a JSON string from a web page, which contains a list of objects. Within each object are two strings, which both contain a link to an image. I want to download this image locally.
I'm currently using an async function which is fired every few seconds, and looks for any new objects within the JSON that weren't previously in the list, gets the image links, and downloads them like so:
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");
}
This works OK until I get a larger volume of images coming in, at which point I sometimes get the error:
The process cannot access the file 'path' because it is being used by another process.
I'm not manipulating the files anywhere else, and although the image path does get bound to an image control, I still get the same error if I break the binding.
What could be causing this?