Reputation: 1199
I know this kind of question was asked many time but I really can't decide what should I use between Thread
or Thread pool
.
I'm creating download manager program which can download simultaneously.
I used to use Thread
for downloading each file and observation for queuing download but there are weird problems when I want to stop downloading(call to Thread.Abort
).
Now, I'm using Task.Run
, everything work fine but I'm worrying about suitability because it may run from a minute to hours(many people say about Thread pool with cpu-intensive work but none say about long running like this).
So, what should I use for long running operation like this?
Edit, I use HttpWebRequest, HttpWebResponse and Stream for downloading because I want to calculate BPS, ETA and throttling(obtain from HttpWebResponse).
Edit2, I use WPF as UI, calculate and rise about 4 PropertyChanged
event, I'm so sorry that I didn't provide enough information at the first place.
Upvotes: 0
Views: 441
Reputation: 4363
I worked on a project that combined aria2 with c# via rpc. This worked exceedingly well. Whether your leverage aria2 or simply glean from its concepts is up to you, but I encourage you to at least take a look.
hint In order to get peak performance, your file server would need to support http range requests.
Here's the manual
To invoke it, you simply need the executable in your path. Then you invoke it.
aria2c "http://host/file.zip"
Here's how to invoke a process from c#.
Upvotes: 1
Reputation: 273244
Should I use Thread or Thread pool for downloading
Neither.
For an efficient approach, use asynchronous I/O. How exactly depends on the API but several interfaces are now compatible with async/await.
For instance,
- WebClient.DownloadFileAsync(Uri, String)
is already efficent
- WebClient.DownloadFileTaskAsync(Uri, String)
is easier to use
With the stream from HttpWebResponse you can use (Fx 4.5 and up)
Task<int> ReadAsync(byte[] buffer,
int offset, int count, CancellationToken cancellationToken)
Upvotes: 2