多线程代理检查器不能正常工作

时间:2017-11-01 00:59:43

标签: vb.net multithreading proxy

我将向您展示一个有效的代码,没有错误!但是,多线程系统并不像井一样工作,线程似乎在等待一个完成下一个之前完成,就像逐个一样。我该如何解决这个问题?

Imports System.Threading
Imports System.IO
Imports System.Net

Public Class Form1
Dim proxies As New List(Of String)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    CheckForIllegalCrossThreadCalls = False
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim threads As Integer = NumericUpDown1.Value
    ThreadPool.SetMaxThreads(threads, threads)
    ThreadPool.SetMinThreads((threads / 2), (threads / 2))
    For Each proxy In proxies
        ThreadPool.QueueUserWorkItem(AddressOf check)
    Next
End Sub
Public Sub check()
    Dim myProxy As WebProxy
    For Each proxy As String In proxies
        Try
            myProxy = New WebProxy(proxy)
            Dim r As HttpWebRequest = WebRequest.Create("http://www.google.com/")
            r.UserAgent = "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.2 Safari/537.36"
            r.Timeout = 3000
            r.Proxy = myProxy
            Dim re As HttpWebResponse = r.GetResponse()
            ListBox1.Items.Add("Working Proxy: " & proxy)
        Catch ex As Exception
            ListBox1.Items.Add("Unresponsive Proxy: " & proxy)
        End Try
    Next
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Dim fo As New OpenFileDialog
    fo.RestoreDirectory = True
    fo.Multiselect = False
    fo.Filter = "txt files (*.txt)|*.txt"
    fo.FilterIndex = 1
    fo.ShowDialog()
    If (Not fo.FileName = Nothing) Then
        Using sr As New StreamReader(fo.FileName)
            While sr.Peek <> -1
                proxies.Add(sr.ReadLine())
            End While
        End Using
    End If
End Sub
End class

P.S。:公共代码

1 个答案:

答案 0 :(得分:0)

至少部分问题是你的Button1处理程序正在为每个代理创建一个线程,然后check方法遍历整个代理列表。也就是说,你有Button1处理程序:

For Each proxy In proxies
    ThreadPool.QueueUserWorkItem(AddressOf check)
Next

并使用check方法:

For Each proxy As String In proxies
    Try
      ` other code here
    Catch
      ...
    End Try
Next

所以每个代理都会检查每个代理。

您需要让check方法获取一个参数:它应该检查的字符串。所以它看起来像这样:

Public Sub check(stateInfo as Object)
    Dim proxyString as String = CType(stateInfo, String)
    Dim myProxy As WebProxy
    Try
        myProxy = New WebProxy(proxyString)
        Dim r As HttpWebRequest = WebRequest.Create("http://www.google.com/")
        r.UserAgent = "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.2 Safari/537.36"
        r.Timeout = 3000
        r.Proxy = myProxy
        Dim re As HttpWebResponse = r.GetResponse()
        ListBox1.Items.Add("Working Proxy: " & proxyString)
    Catch ex As Exception
        ListBox1.Items.Add("Unresponsive Proxy: " & proxyString)
    End Try
End Sub

在你的Button1处理程序中:

For Each proxy In proxies
    ThreadPool.QueueUserWorkItem(AddressOf check, proxy)
Next

另外,我强烈建议您禁用非法跨线程调用的检查。这些天你会咬你的。对这些调用使用同步。