asp.NET异步问题

时间:2016-01-27 00:47:40

标签: asp.net vb.net asynchronous

我第一次尝试使用异步并且已经在使用它一段时间了,我已经尝试了所有可以找到的资源而且我无法让它工作。使用task.delay将其更改为一个非常基本的示例我得到了它的工作,但我的代码库我正在努力。

基本上我需要解析大量网站,我需要它相对较快。下面是我到目前为止,它运行,但它不是异步的。输出显示它们一个接一个地运行,我也确实设置了页面标记async = true。

任何帮助将不胜感激,提前谢谢。

Protected Async Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    Dim dsEvents As New EventsObjects
    EventsReader.Website.GetEventList(dsEvents)

    Response.Write("Start processing:" & Now().ToLongTimeString)
    Dim result = Await GetEventSummariesAsync(dsEvents)
    Response.Write(result)
    Response.Write("<br /><br />End processing:" & Now().ToLongTimeString)
End Sub

Private Async Function GetEventSummariesAsync(ByVal dsEvents As EventsObjects) As Task(Of String)
    Dim debugText As String = ""

    Dim eventCount As Integer = dsEvents.EventDetails.Select("Enabled = 1").Count - 1
    Dim taskList(eventCount) As Task(Of String)
    Dim counter As Integer = 0

    For Each eventRow As EventsObjects.EventDetailsRow In dsEvents.EventDetails.Select("Enabled = 1")
        debugText &= "<br /><br />" & eventRow.EventName & " Start Time: " & Now().ToLongTimeString

        'Await GetEventSummaryAsync(eventRow)

        taskList(counter) = GetEventSummaryAsync(eventRow)
        'taskList(counter) = Task.Run(Function()
        '                                 Return asyncGetEventSummary(eventRow)
        '                             End Function)

        counter += 1

        If counter >= 5 Then Exit For
    Next

    For i = 0 To 4 'taskList.Count - 1
        Dim newRow As String = Await taskList(i)
    Next

    Return debugText
End Function

Private Async Function GetEventSummaryAsync(eventRow As EventsObjects.EventDetailsRow) As Task(Of String)
    Dim dsEvents As New EventsObjects

    Dim EventSummary As HtmlAgilityPack.HtmlDocument = EventsReader.URLContent.EventSummary(eventRow.EventURLName)
    Dim summaryNode = EventSummary.DocumentNode.SelectNodes("//*[@id='results']/tbody/tr")
    If Not summaryNode Is Nothing Then

        For i As Integer = 0 To summaryNode.Count - 1
            Dim summary = summaryNode(i).Elements("td").Select(Function(td) td.InnerText.Trim())

            If dsEvents.EventSummary.Select("EventID = " & eventRow.EventID & " and EventNumber = " & summary(0).ToString).Count = 0 Then
                Dim NewRow As EventsObjects.EventSummaryRow = dsEvents.EventSummary.NewRow()

                NewRow.EventID = eventRow.EventID
                NewRow.EventNumber = summary(0)
                NewRow.EventDate = FormatDate(summary(1))
                NewRow.NumberofRunners = summary(2)

                Return ""
                'asyncDsEvents.EventSummary.Rows.Add(NewRow)

            End If
        Next
    End If

    Return ""
End Function

Private Shared Function FormatDate(DateString As String) As DateTime
    Dim provider As Globalization.CultureInfo = Globalization.CultureInfo.InvariantCulture
    provider = New Globalization.CultureInfo("en-AU")

    Return DateTime.ParseExact(DateString.Split("/")(2) & "-" & DateString.Split("/")(1) & "-" & DateString.Split("/")(0), "yyyy-MM-dd", Globalization.CultureInfo.InvariantCulture)
End Function

0 个答案:

没有答案