VB.NET - ThreadPool队列问题

时间:2016-04-28 11:02:15

标签: vb.net visual-studio visual-studio-2015

(在我意识到Threadpool的问题而不是WebRequest超时后,这个问题几乎完全被重写了。)

如果您想自己测试我的应用,请按clicking here下载,然后点击" Grab"然后在它完成后保存。然后转到Checker选项卡并导入文件。等一下,如果你在下面阅读,你会发现问题。

我有一个包含10,123行代理的示例文件 - 如果我在我的应用程序中使用700个线程导入它(似乎无论我设置了多少个线程),然后等待它几乎完成,它永远不会,但将剩下大约100个检查,并将停止。这使得跟踪线程何时完成检查代理是非常困难的(要启用保存和重新导入按钮)。

(我在下面使用的文件大约有13433个代理,你可以看到它几乎已经完成并结束了) 2

(显示即使有一些左排队,似乎没有新线程正在执行) 3

代码:(略微更新)

'Set a Variable to store the Proxies;
Dim proxies As New List(Of String)

Private Sub ImportButtonChecker_Click(sender As Object, e As EventArgs) Handles ImportButtonChecker.Click

    'Create an Open File Dialog;
    Dim ofd As New OpenFileDialog

    'Get the Thread Count;
    Dim threads As Integer = CheckerThreads.Value

    ofd.RestoreDirectory = True
    ofd.Multiselect = False
    ofd.Filter = "txt files (*.txt)|*.txt"
    ofd.FilterIndex = 1
    ofd.ShowDialog()

    'If a file was Loaded Successfully;
    If (Not ofd.FileName = Nothing) Then

        'Disable the Import Button and Thread Count Box;
        ImportButtonChecker.Enabled = False
        CheckerThreads.Enabled = False

        'Read the file loaded by the OpenFileDialog;
        Using sr As New StreamReader(ofd.FileName)

            'Read the file;
            Dim file As String = sr.ReadToEnd

            'Regex;
            Dim regex As New Regex("[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}:[0-9]{1,4}")

            'Get all the Proxies that was found from the Regex Condition;
            Dim matches As MatchCollection = regex.Matches(file)

            'For Each of the Proxies found from the Regex Condition, Add them to the List of Proxies;
            For Each proxy As Match In matches
                proxies.Add(proxy.ToString)
            Next

        End Using

        'Set the Thread Count;
        ThreadPool.SetMinThreads(threads, threads)
        ThreadPool.SetMaxThreads(threads, threads)
        ServicePointManager.DefaultConnectionLimit = threads
        ServicePointManager.Expect100Continue = True

        'For Each proxy from the Opened File;
        For Each Proxy In proxies

            'Check the Proxy;
            ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf CheckProxy), Proxy)

        Next

    End If

End Sub

Private Function CheckProxy(ByVal Proxy As String) As String

    Try

        'Set the WebRequest URL to Google while setting the UserAgent, Timeout and of course the Proxy;
        Dim init As HttpWebRequest = WebRequest.Create("http://www.google.com")
        init.UserAgent = "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.2 Safari/537.36"
        init.Timeout = 4500
        init.ReadWriteTimeout = 8000
        init.Proxy = New WebProxy(Proxy)

        'Initiate the WebRequest and see if the Proxy can connect fine;
        Dim response As HttpWebResponse = init.GetResponse()

        'Since no Exception has Occured as the Proxy as a Working Proxy;
        ValidProxies.Items.Add(Proxy)

        'Autoscroll;
        ValidProxies.TopIndex = ValidProxies.Items.Count - 1

        Return True

    Catch ex As Exception

        'Autoscroll;
        ValidProxies.TopIndex = ValidProxies.Items.Count - 1

        Return False

    Finally

        'Add it as a Checked Proxy;
        CheckedProxies.Items.Add(Proxy)

        Label3.Text = "Checked: " + CheckedProxies.Items.Count.ToString + " Total: " + proxies.Count.ToString

        'Increment the Progress Bar's Value;
        CheckerGroup.Text = "Valid: " + ValidProxies.Items.Count.ToString + " - Checked: " + CheckedProxies.Items.Count.ToString

        'If it's finished;
        If CheckedProxies.Items.Count.ToString = proxies.Count.ToString Then

            'Re-Enable to Import and Save button;
            ImportButtonChecker.Enabled = True
            SaveButtonChecker.Enabled = True

        End If

    End Try

End Function

0 个答案:

没有答案