DownloadStringAsync 在第一次调用时阻塞线程 14 秒 [英] DownloadStringAsync blocks thread for 14 seconds on first call

查看:25
本文介绍了DownloadStringAsync 在第一次调用时阻塞线程 14 秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这只发生在我的一台机器上.我认为这是一个环境配置问题.所有机器都运行 ESET Smart Security 软件防火墙.有什么想法吗?

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);
        }
    }
}

推荐答案

您的机器配置为执行自动代理检测.

您可以在此处禁用它:

或者,您可以手动覆盖 WebClient 使用的代理.将 WebClient.Proxy 属性 设置为 null 指定不应使用代理.任何显式代理设置都会禁用自动代理检测.

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;

但是,在这种情况下,您应该为用户提供在您的应用程序中配置代理的选项,因为某些用户在访问 Web 时需要使用代理.

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.

这篇关于DownloadStringAsync 在第一次调用时阻塞线程 14 秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆