0

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.

witoong623
  • 1,179
  • 1
  • 15
  • 32

2 Answers2

2

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)
H H
  • 263,252
  • 30
  • 330
  • 514
  • Sorry, I didn't mention this before, I use HttpWebRequest, HttpWebResponse and Stream for downloading because I want to calculate BPS, ETA and throttling(obtain from HttpWebResponse).so, with these work does it cause responsiveness suffers or I can use ConfigureAwait to solve it? – witoong623 Sep 26 '15 at 18:07
  • Agreed, but using [HttpClient](https://www.nuget.org/packages/Microsoft.Net.Http/) is usually recommended over WebClient. – Matt Johnson-Pint Sep 26 '15 at 18:07
  • As I said, the details depend on the API. WebClient was just an easy (for me) example. I'll edit. – H H Sep 26 '15 at 18:09
  • with asynchronous await, Doesn't it cause sluggishness as responsiveness suffers from any calculation? if it cause Can I use ConfigureAwait(false) to solve it? – witoong623 Sep 26 '15 at 18:21
  • That all depends on the surrounding application, you don't give us much to work with. The CPU usage for the calculations should be negligible. – H H Sep 26 '15 at 18:37
  • I'm so sorry that I didn't provide enough information, I tried await on I/O operation and it cause poorly responsiveness, maybe becaues I must calculate and rise about 4 update event(WPF) every time ReadAsync return , but I can fix it by `ConfigureAwait(false)` on hight level method. so, it run on thread in thread pool again, is this difference from `Task.Run` without `ConfigureAwait(false)`? – witoong623 Sep 26 '15 at 19:04
  • You could use 1 or even a few (pool) threads, just not 1 per download when there are many. Reporting progress is a separate issue but the WPF databinding is threadsafe, just use a ViewModel. – H H Sep 26 '15 at 20:19
  • Thank you, finally I knew what cause poorly responsiveness, it because I implemented `ReadAsync` in wrong manner(synchronous block). I haven't noticed this before because I used thread in thread pool. – witoong623 Sep 27 '15 at 16:54
1

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#.

Community
  • 1
  • 1
Josh C.
  • 4,303
  • 5
  • 30
  • 51
  • I already looked into it but as I'm newbie, I really don't know how to use it with C# program, Can you give me a hint so that I can research about using external utility like this? – witoong623 Sep 27 '15 at 17:15