Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DownloadStringAsync blocks thread for 14 seconds on first call

Tags:

c#

webclient

This only happens on one of my machines. I think it's an environment configuration problem. All machines run ESET Smart Security software firewall. Any ideas?

using System;
using System.Net;
using System.Diagnostics;
using System.Threading;

namespace Test
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            bool exit = false;
            WebClient wc = new WebClient();
            DateTime before = DateTime.Now;
            wc.DownloadStringAsync(new Uri("http://74.125.95.147"), "First"); // IP Address of google, so DNS requests don't add to time.
            wc.DownloadStringCompleted += delegate(object sender, DownloadStringCompletedEventArgs e)
            {
                Debug.WriteLine(e.UserState + " Call: " + (DateTime.Now - before));

                if ((string)e.UserState == "First")
                {
                    before = DateTime.Now;
                    wc.DownloadStringAsync(new Uri("http://74.125.95.147"), "Second");
                }
                else
                    exit = true;
            };

            /*
             * 
             * Output:
             * 
             * First Call: 00:00:13.7647873
             * Second Call: 00:00:00.0740042
             * 
             */

            while (!exit)
                Thread.Sleep(1000);
        }
    }
}
like image 686
Mango Avatar asked Jun 27 '10 19:06

Mango


1 Answers

Your machine is configured to perform Automatic Proxy Detection.

You can disable it here:

Screenshot

Alternatively, you can manually override the proxy used by the WebClient. Set the WebClient.Proxy Property to null to specify that no proxy should be used. Any explicit proxy setting disables Automatic Proxy Detection.

client.Proxy = null;

However, you should offer the user the option to configure a proxy in your application in this case, because some users are required to use a proxy when accessing the Web.

like image 177
dtb Avatar answered Nov 14 '22 21:11

dtb