0

我在 VB 中有现有代码,我需要在其中一次性处理列表中的所有记录,并等待他们的响应。当他们响应时,将它们添加到数据表中。该代码有效。我只需要将其转换为异步运行。

这是我的代码:

Imports System.Net
Imports System.IO

Public Class Form1

    Dim _Datatable As New DataTable

    Private Sub btnProcess_Click(sender As System.Object, e As System.EventArgs) Handles btnProcess.Click

        prgProgress.Value = 0
        prgProgress.Maximum = lstUrls.Items.Count

        For Each _ProductEntry As String In lstUrls.Items


            Try

                Dim _Webclient As New WebClient
                '_Webclient.Proxy = _ProxyClient
                Dim _DataStream As Stream = _Webclient.OpenRead(New Uri(_ProductEntry))

                Dim _DataRead As New StreamReader(_DataStream)
                Dim _HtmlContent As String = _DataRead.ReadToEnd
                Dim _HtmlDocument As mshtml.IHTMLDocument2 = New mshtml.HTMLDocument

                _HtmlDocument.write(_HtmlContent)

                Dim _ProductName As mshtml.IHTMLHeaderElement = _HtmlDocument.getElementById("product-header")

                _DataStream.Close()
                _DataRead.Close()

                Call CheckTable("Name")
                Call CheckTable("URL")
                Call CheckTable("Image")
                Call CheckTable("Price")

                Dim _Price As String = String.Empty

                Dim _SpanElements As mshtml.IHTMLElementCollection = _HtmlDocument.getElementsByTagName("span")

                For Each _SpanElement As mshtml.IHTMLSpanElement In _SpanElements
                    If _SpanElement.classname = "regular-price" Then
                        _Price = Replace(Replace(_SpanElement.innertext, "£", ""), "Incl. VAT", "")
                    End If
                Next

                Dim _ImageLocation As String = String.Empty

                For Each _Paragraph As mshtml.IHTMLElement In _HtmlDocument.getElementsByTagName("image")
                    If _Paragraph.id = "product-image-main" Then
                        Dim _Image As mshtml.IHTMLImgElement = CType(_Paragraph, mshtml.IHTMLImgElement)
                        _ImageLocation = _Image.src
                        Exit For
                    End If
                Next


                Dim tableElements As mshtml.IHTMLElementCollection
                tableElements = _HtmlDocument.getElementsByTagName("Table")

                Dim oTableTest As mshtml.IHTMLTable2 = tableElements.item(1)

                'BUILD HEADERS
                For Each _ColumnHeader As mshtml.IHTMLTableRow In oTableTest.rows
                    CheckTable(CType(_ColumnHeader.cells(0), mshtml.IHTMLElement).innerText)
                Next

                Dim _DataRow As DataRow = _DataTable.NewRow
                _DataRow.Item("Name") = CType(_ProductName, mshtml.IHTMLElement).innerText
                _DataRow.Item("URL") = _ProductEntry
                _DataRow.Item("Price") = _Price
                _DataRow.Item("Image") = _ImageLocation

                For Each _RowData As mshtml.IHTMLTableRow In oTableTest.rows

                    Dim _Header As mshtml.IHTMLElement = _RowData.cells(0)
                    Dim _Value As mshtml.IHTMLElement = _RowData.cells(1)
                    _DataRow.Item(_Header.innerText) = _Value.innerText
                Next

                Dim _Elements As mshtml.IHTMLElementCollection = _HtmlDocument.all
                For Each _Element As mshtml.IHTMLElement In _Elements
                    If _Element.className = "product-description product-documents" Then
                        For Each _ProductLink As mshtml.IHTMLElement In _Element.all
                            If _ProductLink.tagName = "A" Then
                                CheckTable(_ProductLink.innerText)
                                _DataRow(_ProductLink.innerText) = Replace(CType(_ProductLink, mshtml.IHTMLAnchorElement).href, "about:", "http://www.tapoutlet.co.uk")
                            End If
                        Next
                    End If
                Next

                _DataTable.Rows.Add(_DataRow)
                _DataTable.AcceptChanges()

                dgvScrapedData.DataSource = _Datatable
                dgvScrapedData.Refresh()

            Catch ex As Exception
                Console.WriteLine("Error getting webpage-" & _ProductEntry)
                Console.WriteLine(ex.Message.ToString)
            End Try

        Next
    End Sub

    Private Function CheckTable(ByVal ColumnName As String) As Boolean

        If _DataTable.Columns.Contains(ColumnName) Then
            Return True
        Else
            _DataTable.Columns.Add(ColumnName)
            Return False
        End If

    End Function
End Class
4

0 回答 0