Downloading large file
Hello, I need to be able to download files up to 400Mb asynchronously and track the download in a progress bar. I have this code except that it doesn't work totally, on 50Mb files it's fine but 130Mb it crashes and moreover it takes a while before starting the download. So I wanted to know if this was the right method and if it was possible to optimize it?
46 Replies
You need to pass
HttpCompletionOption.ResponseHeadersRead
I think, stop it downloading everything at once?
Also that buffer you get back from the ArrayPool might be significantly bigger than what you asked for
There's no point trying to asynchronously write to a file if the file wasn't opened in async mode - it does nothingHowever I tried to use
Write
instead and it was much slower
How do I know to what extent?It shouldn't be. It still does an sync write, but it does it on a ThreadPool thread
You can look at the size of the Memory you get back, but it's unspecified. You can slice the Memory to the right size if you depend on that
How is that relevant, it should just potentially increase performance
Also you might end up calling
progress.Increment
for every byte received, if ReadAsync ends up giving them to you one by one, which might well overload other parts of your app
They might end up reporting progress too infrequentlyYou could make sure the whole buffer is filled before writing it to the file
Well, I have the impression that the progress only increases at the end of the file download.
Also using a buffer that's too large might not play well with the cache
Did you try using ResponseHeadersRead?
That was my first suggestion, and probably the cause of your problem
The other things I mentioned are just other problems I spotted
It's miraculous, thank you very much
$close
If you have no further questions, please use /close to mark the forum thread as answered
Glad to hear!
Um, in fact it starts faster and the progress bar reacts well but on the other hand I have the impression that overall it's slower
It probably will be: it's doing more work than it was before
Try using a smaller buffer (1024 or so), and keep calling ReadAsync until the buffer is (nearly) full
(before calling progress.Increment and flushing the buffer to file)
And even if it crashes I still have these errors in the console
Exception thrown: 'System.IO.IOException' in System.Private.CoreLib.dll
Exception thrown: 'System.Net.Http.HttpRequestException' in System.Net.Http.dll
Exception thrown: 'System.Net.Http.HttpRequestException' in System.Private.CoreLib.dll
Exception thrown: 'System.Net.Http.HttpRequestException' in System.Net.Http.dll
Exception thrown: 'System.Net.Http.HttpRequestException' in System.Private.CoreLib.dll
Exception thrown: 'System.Net.Http.HttpRequestException' in System.Private.CoreLib.dll
Exception thrown: 'System.Net.Http.HttpRequestException' in System.Private.CoreLib.dll
Exception thrown: 'System.Net.Http.HttpRequestException' in System.Private.CoreLib.dll
Exception thrown: 'System.Net.Http.HttpRequestException' in System.Net.Http.dll
Exception thrown: 'System.Net.Http.HttpRequestException' in System.Net.Http.dll
Exception thrown: 'System.Net.Http.HttpRequestException' in System.Private.CoreLib.dll
Exception thrown: 'System.Net.Http.HttpRequestException' in System.Private.CoreLib.dll
Exception thrown: 'System.Net.Http.HttpRequestException' in System.Net.Http.dll
Exception thrown: 'System.Net.Http.HttpRequestException' in System.Private.CoreLib.dll
Exception thrown: 'System.Threading.Tasks.TaskCanceledException' in System.Private.CoreLib.dll
Exception thrown: 'System.Threading.Tasks.TaskCanceledException' in System.Private.CoreLib.dll
Exception thrown: 'System.Threading.Tasks.TaskCanceledException' in System.Net.Http.dll
Exception thrown: 'System.Threading.Tasks.TaskCanceledException' in System.Private.CoreLib.dll
Exception thrown: 'System.OperationCanceledException' in System.Private.CoreLib.dll
Exception thrown: 'System.OperationCanceledException' in System.Private.CoreLib.dll
Exception thrown: 'System.OperationCanceledException' in System.Private.CoreLib.dll
Also make sure you're not on a UI thread or anything - I don't know how this method is being called
From the main of a console application
That looks exciting. Open the exceptions window, tell the debugger to break when those exceptions happen, see what's going on
How can I open it?
Debug - Windows iirc?
I don't have anything close to it (sorry it's in French)
Second one
Does this mean that the problem comes from the remote server?
Probably?
Is there a way to confirm this?
I'd probably reach for Wireshark, see what's actually going on
I installed it but I have no idea how to do it
I'm afraid I'm a bit busy. You might have to learn a bit how tcp works
Thanks, I'll take care of other things in the meantime
Unknown User•2w ago
Message Not Public
Sign In & Join Server To View
No, but I know the owner
Unknown User•2w ago
Message Not Public
Sign In & Join Server To View
But is it something that happens only during long sessions of use?
Unknown User•2w ago
Message Not Public
Sign In & Join Server To View
Could it be caused by cloudflare?
Unknown User•2w ago
Message Not Public
Sign In & Join Server To View
How can I do it?
Which bit of that are you struggling with?
keep calling ReadAsync until the buffer is (nearly) full
What about that don't you understand?
Yes, I understand roughly what that means, I've just never worked with memory management and so I don't know how to do that in C#
Call ReadAsync in a loop until the buffer it's reading into is (nearly) full
But at this point, how can I continue my condition in the other loop?
I try this, but it don't work
Something like that?
Yes, thank you, but I have the impression that the download is even slower
Could this be impacted by the fact that my program downloads lots of files at the same time?
Oh, a new error to add to my pokedex