Visual Basic - 如何执行代码的问题

时间:2016-04-08 09:48:24

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

所以我有一个按钮,可以从公共网站中搜索代理并将代理添加到列表框中,但这是一个问题,它出于某种原因显示列表框中的代理一旦实际完成所有&#34 ;对于每个"'

它甚至没有显示进度条,直到一切都完成,这使得它毫无意义。

我的意思截图:

1

(正如你所看到的那样,只有当它完成时才显示进度条而不是它的刮擦。)

有没有解决方法呢?

我的代码:

Imports System.Text.RegularExpressions

Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Button1.Enabled = False
        ScrapeProgress.Visible = True

        'Set the Time;
        Dim time = TimeOfDay.ToString("h:mm:ss tt")

        'Sources;
        Dim sources(122) As String
        sources(0) = "http://proxy-ip-list.com"

        sources(1) = "http://fineproxy.org/eng/?p=6#more-6"

        sources(2) = "http://aliveproxy.com/high-anonymity-proxy-list"
        sources(3) = "http://aliveproxy.com/anonymous-proxy-list/"
        sources(4) = "http://aliveproxy.com/transparent-proxy-list"
        sources(5) = "http://aliveproxy.com/socks5-list"
        sources(6) = "http://aliveproxy.com/socks4-list"
        sources(7) = "http://aliveproxy.com/fastest-proxies/"

        sources(8) = "http://atomintersoft.com/anonymous_proxy_list"
        sources(9) = "http://atomintersoft.com/high_anonymity_elite_proxy_list"
        sources(10) = "http://atomintersoft.com/proxy_list_domain"
        sources(11) = "http://atomintersoft.com/proxy_list_port"
        sources(12) = "http://atomintersoft.com/transparent_proxy_list"
        sources(13) = "http://atomintersoft.com/products/alive-proxy/socks5-list"

        sources(14) = "http://best-proxy.com/english/search.php?search=anonymous-and-elite&country=any&type=anonymous-and-elite&port=any&ssl=any"
        sources(15) = "http://best-proxy.com/english/search.php?search=anonymous-and-elite&country=any&type=anonymous-and-elite&port=any&ssl=any&p=2"
        sources(16) = "http://best-proxy.com/english/search.php?search=anonymous-and-elite&country=any&type=anonymous-and-elite&port=any&ssl=any&p=3"

        sources(17) = "http://www.samair.ru/proxy/socks01.htm"
        sources(18) = "http://www.samair.ru/proxy/socks02.htm"
        sources(19) = "http://www.samair.ru/proxy/socks03.htm"
        sources(20) = "http://www.samair.ru/proxy/socks04.htm"
        sources(21) = "http://www.samair.ru/proxy/socks05.htm"

        sources(22) = "http://www.samair.ru/proxy/proxy-01.htm"
        sources(23) = "http://www.samair.ru/proxy/proxy-02.htm"
        sources(24) = "http://www.samair.ru/proxy/proxy-03.htm"
        sources(25) = "http://www.samair.ru/proxy/proxy-04.htm"
        sources(26) = "http://www.samair.ru/proxy/proxy-05.htm"
        sources(27) = "http://www.samair.ru/proxy/proxy-06.htm"
        sources(28) = "http://www.samair.ru/proxy/proxy-07.htm"
        sources(29) = "http://www.samair.ru/proxy/proxy-08.htm"
        sources(30) = "http://www.samair.ru/proxy/proxy-09.htm"
        sources(31) = "http://www.samair.ru/proxy/proxy-10.htm"
        sources(32) = "http://www.samair.ru/proxy/proxy-11.htm"
        sources(33) = "http://www.samair.ru/proxy/proxy-12.htm"
        sources(34) = "http://www.samair.ru/proxy/proxy-13.htm"
        sources(35) = "http://www.samair.ru/proxy/proxy-14.htm"
        sources(36) = "http://www.samair.ru/proxy/proxy-15.htm"
        sources(37) = "http://www.samair.ru/proxy/proxy-16.htm"
        sources(38) = "http://www.samair.ru/proxy/proxy-17.htm"
        sources(39) = "http://www.samair.ru/proxy/proxy-18.htm"
        sources(40) = "http://www.samair.ru/proxy/proxy-19.htm"
        sources(41) = "http://www.samair.ru/proxy/proxy-20.htm"
        sources(42) = "http://www.samair.ru/proxy/proxy-21.htm"
        sources(43) = "http://www.samair.ru/proxy/proxy-22.htm"
        sources(44) = "http://www.samair.ru/proxy/proxy-23.htm"
        sources(45) = "http://www.samair.ru/proxy/proxy-24.htm"
        sources(46) = "http://www.samair.ru/proxy/proxy-25.htm"
        sources(47) = "http://www.samair.ru/proxy/proxy-26.htm"
        sources(48) = "http://www.samair.ru/proxy/proxy-27.htm"
        sources(49) = "http://www.samair.ru/proxy/proxy-28.htm"
        sources(50) = "http://www.samair.ru/proxy/proxy-29.htm"
        sources(51) = "http://www.samair.ru/proxy/proxy-30.htm"

        sources(52) = "http://nntime.com/proxy-list-01.htm"
        sources(53) = "http://nntime.com/proxy-list-02.htm"
        sources(54) = "http://nntime.com/proxy-list-03.htm"
        sources(55) = "http://nntime.com/proxy-list-04.htm"
        sources(56) = "http://nntime.com/proxy-list-05.htm"
        sources(57) = "http://nntime.com/proxy-list-06.htm"
        sources(58) = "http://nntime.com/proxy-list-07.htm"
        sources(59) = "http://nntime.com/proxy-list-08.htm"
        sources(60) = "http://nntime.com/proxy-list-09.htm"
        sources(61) = "http://nntime.com/proxy-list-10.htm"
        sources(62) = "http://nntime.com/proxy-list-11.htm"
        sources(63) = "http://nntime.com/proxy-list-12.htm"
        sources(64) = "http://nntime.com/proxy-list-13.htm"
        sources(65) = "http://nntime.com/proxy-list-14.htm"
        sources(66) = "http://nntime.com/proxy-list-15.htm"
        sources(67) = "http://nntime.com/proxy-list-16.htm"
        sources(68) = "http://nntime.com/proxy-list-17.htm"
        sources(69) = "http://nntime.com/proxy-list-18.htm"
        sources(70) = "http://nntime.com/proxy-list-19.htm"
        sources(71) = "http://nntime.com/proxy-list-20.htm"
        sources(72) = "http://nntime.com/proxy-list-21.htm"
        sources(73) = "http://nntime.com/proxy-list-22.htm"
        sources(74) = "http://nntime.com/proxy-list-23.htm"
        sources(75) = "http://nntime.com/proxy-list-24.htm"
        sources(76) = "http://nntime.com/proxy-list-25.htm"
        sources(77) = "http://nntime.com/proxy-list-26.htm"
        sources(78) = "http://nntime.com/proxy-list-27.htm"
        sources(79) = "http://nntime.com/proxy-list-28.htm"
        sources(80) = "http://nntime.com/proxy-list-29.htm"
        sources(81) = "http://nntime.com/proxy-list-30.htm"

        sources(82) = "http://proxylistchecker.org/proxylists.php?t=&p=1"
        sources(83) = "http://proxylistchecker.org/proxylists.php?t=&p=2"
        sources(84) = "http://proxylistchecker.org/proxylists.php?t=&p=3"
        sources(85) = "http://proxylistchecker.org/proxylists.php?t=&p=4"
        sources(86) = "http://proxylistchecker.org/proxylists.php?t=&p=5"
        sources(87) = "http://proxylistchecker.org/proxylists.php?t=&p=6"
        sources(88) = "http://proxylistchecker.org/proxylists.php?t=&p=7"
        sources(89) = "http://proxylistchecker.org/proxylists.php?t=&p=8"
        sources(90) = "http://proxylistchecker.org/proxylists.php?t=&p=9"
        sources(91) = "http://proxylistchecker.org/proxylists.php?t=&p=10"
        sources(92) = "http://proxylistchecker.org/proxylists.php?t=&p=11"
        sources(93) = "http://proxylistchecker.org/proxylists.php?t=&p=12"
        sources(94) = "http://proxylistchecker.org/proxylists.php?t=&p=13"
        sources(95) = "http://proxylistchecker.org/proxylists.php?t=&p=14"
        sources(96) = "http://proxylistchecker.org/proxylists.php?t=&p=15"

        sources(97) = "http://txt.proxyspy.net/proxy.txt"

        sources(98) = "http://www.getproxy.jp/en/default/1"
        sources(99) = "http://www.getproxy.jp/en/default/2"
        sources(100) = "http://www.getproxy.jp/en/default/3"
        sources(101) = "http://www.getproxy.jp/en/default/4"
        sources(102) = "http://www.getproxy.jp/en/default/5"

        sources(103) = "http://www.ip-adress.com/proxy_list/?k=time&d=desc"

        sources(104) = "http://www.my-proxy.com/free-proxy-list.html"
        sources(105) = "http://www.my-proxy.com/free-proxy-list-2.html"
        sources(106) = "http://www.my-proxy.com/free-proxy-list-3.html"
        sources(107) = "http://www.my-proxy.com/free-proxy-list-4.html"
        sources(108) = "http://www.my-proxy.com/free-proxy-list-5.html"
        sources(109) = "http://www.my-proxy.com/free-proxy-list-6.html"
        sources(110) = "http://www.my-proxy.com/free-proxy-list-7.html"
        sources(111) = "http://www.my-proxy.com/free-proxy-list-8.html"
        sources(112) = "http://www.my-proxy.com/free-proxy-list-9.html"
        sources(113) = "http://www.my-proxy.com/free-proxy-list-10.html"

        sources(114) = "http://sslproxies24.blogspot.com/feeds/posts/default"
        sources(115) = "http://proxyserverlist-24.blogspot.com/feeds/posts/default"
        sources(116) = "http://newfreshproxies24.blogspot.com/feeds/posts/default"
        sources(117) = "http://socksproxylist24.blogspot.com/feeds/posts/default"
        sources(118) = "http://vip-socks24.blogspot.com/feeds/posts/default"
        sources(119) = "http://irc-proxies24.blogspot.com/feeds/posts/default"
        sources(120) = "http://www.socks24.org/feeds/posts/default"

        sources(121) = "http://www.getproxy.jp/en/"
        sources(122) = "http://www.getproxy.jp/en/fastest"

        'Add the Copyright Holder to the top of the List;
        ListBox1.Items.Add("Proxies Scraped using a Test Build Program by ShinyMK")

        'Foreach Source, Do the following Code;
        For Each element As String In sources

            'Set the Status Label to "Scraping";
            ToolStripStatusLabel3.Text = "Scraping"

            'Connect to the Proxy Source;
            Dim source As Net.HttpWebRequest = Net.WebRequest.Create(element)

            Try
                'Prepare the Response;
                Dim response As Net.HttpWebResponse = source.GetResponse

                'Load the HTML;
                Dim reader As IO.StreamReader = New IO.StreamReader(response.GetResponseStream())
                Dim html As String = reader.ReadToEnd

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

                'Set the Matches variable to the Matched Sections of the HTML source;
                Dim matches As MatchCollection = expression.Matches(html)

                'Add the proxies to the ListBox;
                For Each proxy As Match In matches

                    'If the ListBox doesn't already contain the Proxy, Add It;
                    If Not ListBox1.Items.Contains(proxy.ToString) Then

                        'Add the Proxy;
                        ListBox1.Items.Add(proxy.ToString)

                        'Add 1 to the "Count" label's value;
                        ToolStripStatusLabel5.Text += 1

                    End If

                Next

                'Add the Time and Success message for each Successful Scraped Source;
                ListBox2.Items.Add("[" + time + "] Success: " + element)

            Catch ex As Exception

                ListBox2.Items.Add("[" + time + "] ERROR Scraping from " + element + " - " + ex.Message)

            End Try

        Next

        'Re-Enable the Button and set the Status Lable to "Idle";
        Button1.Enabled = True
        ToolStripStatusLabel3.Text = "Idle"

    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

        If ListBox1.Items.Count = (0) Then
            MessageBox.Show("No Proxies Scraped.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Else
            'defining a streamwriter
            Dim S_W As IO.StreamWriter
            'converting listbox items to string
            Dim itms() As String = {ListBox1.Items.ToString}
            ''defining a savefiledialog
            Dim save As New SaveFileDialog
            Dim it As Integer
            save.FileName = "SSLPro.xyz " + TimeOfDay.ToString("h.mm.ss tt")
            save.Filter = "Text (*.txt)|*.txt|All Files (*.*)|*.*"
            save.CheckPathExists = True
            save.ShowDialog(Me)
            S_W = New IO.StreamWriter(save.FileName)
            For it = 0 To ListBox1.Items.Count - 1
                S_W.WriteLine(ListBox1.Items.Item(it))
            Next
            S_W.Close()
        End If

    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click

        ListBox1.Items.Clear()
        ListBox2.Items.Clear()
        ToolStripStatusLabel5.Text = "0"

    End Sub

End Class 

1 个答案:

答案 0 :(得分:0)

要在您遍历代理时允许UI线程刷新并显示信息,您需要使用单独的线程来完成工作。

您可以使用.NET控件Backgroundworker执行此操作。使用此控件,您可以启动一个线程来完成工作,并且该线程可以使用Brackgroundworker的事件通知UI线程操作状态,然后UI线程可以相应地更新UI。

Here您可以找到一些如何使用背景工作者的示例。

这个基本样本可能是这样的:

Imports System.ComponentModel

Public Class Form1

    Private ListOfProcessedElements as List(of integer)


    Private Sub backgroundWorker1_DoWork(ByVal sender As System.Object,ByVal e As DoWorkEventArgs) Handles backgroundWorker1.DoWork
        'Here you do your worker

        'Add an element to the list of Processed elements
        ListOfProcessedElements.add(ANY_NUMBER)


        worker.ReportProgress(i * 10)
    End Sub

    ' This event handler updates the progress.
    Private Sub backgroundWorker1_ProgressChanged(ByVal sender As System.Object, ByVal e As ProgressChangedEventArgs) Handles backgroundWorker1.ProgressChanged
        'Here you can access to the shared variable ListOfProcessedElements and do whatever you want in the UI 
    End Sub

    ' This event handler deals with the results of the background operation.
    Private Sub backgroundWorker1_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As RunWorkerCompletedEventArgs) Handles backgroundWorker1.RunWorkerCompleted

    End Sub

End Class