WebClientDownloadFileTaskAsync file in use, even with Dispose

1

I'm downloading 10 images at the same time async, but in the 2nd photo usually the error that the image is in use.

  

The process can not access the file because it is being used by   another process.

I already think it's strange, because the old code that moved the images after the download was already removed, that is, I'm not doing anything beyond the download, so I should not give this error.

My code looks like this:

    public static async Task DownloadImgAsync(IEnumerable<FotosProduto> urls, int id)
    {
//recebe uma lista com 10 urls para download
            var urlTasks = urls.Select((url, index) =>
            {
                using (var wc = new WebClient())
                {
                    var urlTratada = url.Url.Replace(" ", "").Replace("\n", "");
//garantindo que a url está correta
                    var path = Caminhos.FolderImg + url.FileNameFinal;
//aqui só recupero a pasta default das minhas imagens + o nome final da foto
                    var downloadTask = wc.DownloadFileTaskAsync(new Uri(urlTratada), path);
//insere o processo na task de download
                    wc.Dispose(); //redudante pelo using, mas ainda sim pra tentar evitar o erro.
                    return downloadTask;
                }
            });

            await Task.WhenAll(urlTasks);
    }

See that I have already put it with using and for disclaimer I still call dispose , however it always returns that the image is in use. how?

NOTE: This function is called for each product, usually I have a large list of products running eg 1000mil products and each product 10 images.

Other questions from me regarding the same project:

Download Async + Copy = Copying image 0 bytes

await Task.When All As to run multiple processes?

    
asked by anonymous 20.10.2016 / 13:48

1 answer

1

The problem was for some reason my function of generating random name was not so random like this:

  public string GerarNomeJPG(int _size)
        {
            var random = new Random((int)DateTime.Now.Ticks);
            var builder = new StringBuilder();
            char ch;
            for (int i = 0; i < _size; i++)
            {
                ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
                builder.Append(ch);
            }
            builder.Append(".jpg");
            return builder.ToString().ToLower();
        }

Solution thanks to the comment from @Ricardo

    
20.10.2016 / 15:43