2014-10-05 102 views
0

我一直在爲此撓頭。我想知道如何一次測試多個代理,我知道如何一次測試一個代理,但這需要很多時間。 我使用這個代碼來測試代理C#測試多個代理

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

測試代理功能

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

回答

0

你可以試試這個(多線程)

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

嘿,謝謝你這麼快回答。我試過這個,我遇到了這個部分的一些問題 Thread TestProxy = new Thread(testProxy(proxy,port,i)); TestProxy.Start(); 它首先說它不能從bool轉換爲'System.Threading.ParameterizedStart'然後我改變它爲void,它說的是同樣的事情,但用void代替bool – user3528008 2014-10-05 11:08:50

+0

@ user3528008你用private void testProxy(string proxy ,int port,int listpos)? – Luis 2014-10-05 12:08:45

+0

nope,public void testPorxy(string proxy,int port,int listpos) – user3528008 2014-10-05 14:08:41