views:

249

answers:

3

I'm using the DownloadFileAsync method of WebClient to download some files from a server, and I can't help but notice that in my informal testing of my code within VS2010, it blocks for about 3 seconds while it starts, which, in my opinion kind of defeats the purpose in the first place.

Here is the relevant snippet of code:

WebClient downloader = new WebClient();
downloader.DownloadProgressChanged += new DownloadProgressChangedEventHandler(updateDownloadProgress);
downloader.DownloadFileCompleted += new System.ComponentModel.AsyncCompletedEventHandler(downloadCompleted);

var current_map = map_downloads[0];//string with filename, map_downloads is List<string>

var path = System.IO.Path.GetTempFileName();

downloaded_maps.Add(path);//adding the temp file to a List<string>

downloader.DownloadFileAsync(new Uri(MAP_BASE + current_map), path); //MAP_BASE is a string containing the base url

I'm using DownloadFileAsync to keep the UI from blocking while the application downloads a ~100 MB file. Obviously if the UI blocks for 3 seconds while the call starts, that diminishes the utility somewhat, if not entirely.

I'm relatively inexperienced with C#/.Net (I did a bunch of .Net 2.0 stuff about 3-4 years ago, IIRC, but I'm basically relearning it now).

A: 

Have you figured out if the delay is in your app, or on the network? To find out if the destination server is slow, run Wireshark and see when the first response is received after request is sent. Maybe that is where the delay occurs?

Also, if this is part of a big application, then the first time will always be slow due to startup costs. If you really want to get a good measurement, measure the total time required for the 1st invocation, and for the 2nd through the 10th invocation. From this you can find out if the delay is in startup costs, or everytime.

feroze
+2  A: 

I read somewhere that DownloadFileAsync actually checks the DNS name in a blocking thread, hence why you may be getting the slowdown. If you put the IP in directly, there should be no blocking. Found that piece of info here: http://www.csharp-examples.net/download-files/

Nav
+1  A: 

In addition to what Nav says, it looks like the problem is web proxy autodetection, see this answer: http://stackoverflow.com/questions/754333/why-is-this-webrequest-code-slow/935728#935728

I tested it and it now works without any significant delay during the first call.

Igor Brejc