WebClient does not support concurrent I/O operations

前端 未结 4 2144
温柔的废话
温柔的废话 2020-11-30 09:45

How can I get this error from with in the DownloadStringCompleted Event? Doesn\'t that mean, it\'s finished? Is there another event I can fire this from?

I get this

4条回答
  •  暖寄归人
    2020-11-30 10:15

    Instead of using WebClient use HttpClient to do parallel HTTP calls. Below code shows how to download files.

            HttpClient httpClient = new HttpClient();
            var documentList=_documentManager.GetAllDocuments();
            documentList.AsParallel().ForAll(doc =>
            {
    
                var responseResult= httpClient.GetAsync(doc.FileURLPath);
                using (var memStream = responseResult.Result.Content.ReadAsStreamAsync().Result)
                {
                    using (var fileStream =File.Create($"{filePath}\\{doc.FileName}"))
                    {
                        memStream.CopyTo(fileStream);
                    }
    
                }
            });
    

提交回复
热议问题