C#测试多个代理

C#测试多个代理,c#,multithreading,proxy,C#,Multithreading,Proxy,我一直在为这事挠头。我想知道如何一次测试多个代理,我知道如何一次测试一个代理,但这需要很多时间。 我正在使用此代码测试代理 public bool testProxy( string proxy, int port ) { try { WebClient web = new WebClient(); web.Proxy = new WebProxy( proxy, port);

我一直在为这事挠头。我想知道如何一次测试多个代理,我知道如何一次测试一个代理,但这需要很多时间。 我正在使用此代码测试代理

    public bool testProxy( string proxy, int port )
    {
        try
        {
            WebClient web = new WebClient();
            web.Proxy = new WebProxy( proxy, port);
            web.DownloadString("http://www.google.com/ncr");
            return true;
        }
        catch
        {
            return false;
        }
    }
现在,我该如何使用多线程或我需要使用的任何东西来测试它们? 因为现在我是在按下按钮的时候这样做的,这很耗时

        if (proxy_list.Count > 0)
        {
            for (int i = 0; i < proxy_list.Count; i++)
            {
                string Proxy = proxy_list[i];
                string[] vars = Proxy.Split( ':' );
                if (vars.Length == 2)
                {
                    proxy = vars[0];
                    port = int.Parse(vars[1]);
                    if ( !testProxy( proxy, port ) )
                    {
                        proxy_list.RemoveAt(i);
                    }
                }
                else
                {
                    proxy_list.RemoveAt(i);
                }
                textBox3.Text = proxy_list.Count.ToString();
                this.Refresh();
            }
        }
你可以试试这个(多线程)

if(proxy_list.Count>0)
{
对于(int i=0;i
嘿,谢谢你这么快回答。我尝试了这一点,但在这个部分中遇到了一些问题:线程TestProxy=newthread(TestProxy(proxy,port,I));TestProxy.Start();它首先说它不能从bool转换为'System.Threading.ParameterizedStart',然后我把它改为void,它说了同样的话,但是用void代替了bool@user3528008您使用了私有void testProxy(字符串代理,int-port,int-listpos)?否,公共void testPorxy(字符串代理,int-port,int-listpos)
    public void testProxy(string proxy, int port, int listpos)
    {
        try
        {
            WebClient web = new WebClient();
            web.Proxy = new WebProxy(proxy, port);
            web.DownloadString("http://www.google.com/ncr");
        }
        catch
        {
            proxy_list.RemoveAt(listpos);
        }
    }
        if (proxy_list.Count > 0)
        {
        for (int i = 0; i < proxy_list.Count; i++)
        {
            string Proxy = proxy_list[i];
            string[] vars = Proxy.Split( ':' );
            if (vars.Length == 2)
            {
                proxy = vars[0];
                port = int.Parse(vars[1]);

            Thread TestProxy = new Thread(testProxy( proxy, port, i));
            TestProxy.Start();
            }
            else
            {
                proxy_list.RemoveAt(i);
            }
            textBox3.Text = proxy_list.Count.ToString();
            this.Refresh();
        }
    }



    public bool testProxy( string proxy, int port, int listpos )
    {
    try
    {
        WebClient web = new WebClient();
        web.Proxy = new WebProxy( proxy, port);
        web.DownloadString("http://www.google.com/ncr");
        return true;
    }
    catch
    {
        proxy_list.RemoveAt(listpos);
    }
}