1

After googling, I found out there's no method to limit download speed in WebClient class.

So now I'm thinking of putting Thread.Sleep() in the method of DownloadFileAsync()'s DownloadProgressChangedEventHandler.

I guess this way would work anyway but I'm not sure if I can still call it bandwidth throttling.

Let's say there are two downloaders. The first one downloads 50Bytes per sec. The second one downloads 100Bytes per 2 secs. Then both are 50Bps.

As you know, what I'm trying to do with WebClient is the second way. After downloading 100bytes, sleep for a sec, and then download 100Bytes, and sleep for a sec again...

Will my approach be okay?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Jenix
  • 2,996
  • 2
  • 29
  • 58
  • 1
    You can look at very good example of Bandwidth throttling at http://www.codeproject.com/Articles/18243/Bandwidth-throttling – Vivek Mishra Dec 24 '15 at 12:07
  • @VIVEK I've already read it but need to stick to `DownloadFileAsync()` of `WebClient` for now. Thanks though! – Jenix Dec 24 '15 at 12:21
  • 1
    You cant throttle DownloadFileAsync. You could do something at the network level but it will be kludgy. Suggest you try port your code to a chunked download that you can control. – Murray Foxcroft Dec 24 '15 at 12:25
  • @MurrayFoxcroft Thanks! What do you think of my approach above? Is it a bad idea? – Jenix Dec 24 '15 at 12:33
  • Test it, but I doubt it's going to make any difference. The event handler is just that, an event being raised and I cant see anything in your handler blocking the actual download stream. Why not use an alternative approach - what is limiting you from doing something like http://www.codeproject.com/Articles/18243/Bandwidth-throttling – Murray Foxcroft Dec 24 '15 at 12:42
  • @MurrayFoxcroft Well, calling `Thread.Sleep(x)` in the event method of `DownloadFileAsync()` can hold the download thread for x. I tested this and got what I expected as I described above. But I'm not sure this is good way to do. As for your question, of course I can try that too, but it takes time for now.. – Jenix Dec 24 '15 at 13:03
  • 1
    Are you sure it's holding the download thread (the actual file download) and not just the progress reporting? If it's working and it's a quick and dirty throw away app then stick with it. If you are building something that you plan to use for some time, then I'd opt for a neater approach. – Murray Foxcroft Dec 24 '15 at 13:15
  • @MurrayFoxcroft As for your question, yes I am! That's why we shouldn't put some heavy jobs there generally. And as for your suggestion, I agree as well, thanks :) – Jenix Dec 24 '15 at 13:26

1 Answers1

0

I have tried with the solution of Thread.Sleep(x)... if u put a long sleep and see in the task manager with the network monitor.. you will see that the application consumes full bandwidth and does not hold the Download thread.

Qasim Shafi
  • 69
  • 1
  • 5
  • Well, give another try with a big size file. I'm pretty sure, if you put an infinite loop in the DownloadProgressChangedEventHandler method, you'll never get the full file from the download thread. But I know what you mean. When your program just began downloading, it'll download some amount of data at full bandwidth, but after that, it'll stop. That's why I said you should try with a big file. – Jenix Jun 19 '16 at 13:16
  • 1
    I know its too late to answer, but I was downloading a 6GB file from one of our servers back then. – Qasim Shafi Oct 22 '19 at 22:05
  • Wow, really late reply! I totally forgot about this lol. Yeah, you are right. Firstly, right after posting this question, I just realized this approach was stupid and useless in the first place. And secondly, I found a mistake in my test code and misinterpreted it. – Jenix Oct 23 '19 at 13:01