Reputation: 34294
I want to download a file while displaying on UI the download is in progress. With WebRequest
I have two options:
Use WebRequest.BeginGetResponse
and related methods.
Advantage: possibility to display exact progress in bytes.
Disadvantage: more code to write. As synchronous and asynchronous methods don't mix, I'll need to use BeginGetResponse
, BeginRead
, BeginGetRequestStream
. More things to implement manually, including read buffers, timeouts.
Start new thread (or use thread pool), use WebRequest.GetResponse
and related methods.
Advantage: no unnecessary code.
Disadvantage: impossible to show exact progress.
As files to be downloaded are rather small, displaying progress in bytes is advantage, but not crucial. Do I miss something if I use second approach, feature-wise and performance-wise?
Or maybe there's a higher-level widely used wrapper I missed? (WebClient
doesn't expose WebRequest
's properties, so it's unusable for me, because I need to use cookies etc.)
Upvotes: 2
Views: 2788
Reputation: 47058
If you create a new thread (option no 2) that thread will block while doing the request. If you only make one request at a time that won't matter much, but if you do lots of requests you might end up with lots of extra threads. Each thread will cost you ~1MB RAM.
Asynchronous calls like WebRequest.BeginGetResponse
will not block any of your threads while the request is in process, it will only pick a thread from the thread pool when your request is completed to deliver the result.
Upvotes: 4