简体   繁体   中英

Webpage never finishes loading asynchronously

Please see the code below:

    Public Shared Async Sub AsyncMain()
        Dim a As AsyncronousTest = New AsyncronousTest
        Dim task As Task = a.LoadWebpage()

        Do While 1 = 1
            If a.bool = True Then
                Exit Do
            End If
        Loop
        MsgBox("test")
    End Sub

Public Class AsyncronousTest

    Public bool As Boolean = False

    Public Sub Callback()
        bool = True
    End Sub

    Public Async Function LoadWebpage() As Task(Of Integer)
        Dim webC As WebClient = New WebClient()
        Dim newUri As Uri = New Uri("http://webpagetocache")
        Dim task1 As Task = webC.DownloadStringTaskAsync(newUri)
        Await task1
        Callback()
        Return 1
    End Function
End Class

msgbox("test") is never reached. Why?

The purpose of this is to load a webpage overnight so that it can be cached using asp.net.

You are calling LoadWebPage and it is returning the task, but this task is never performed, thus the actual code inside LoadWebPage is never executed. You need to perform the task to execute it.

Public Async Sub AsyncMain()
    Dim a As AsyncronousTest = New AsyncronousTest
    Dim task As Task(Of Integer) = a.LoadWebpage() ' Here you generate the task

    Dim actualResult = Await task ' Here you perform the task

    Do While 1 = 1
        If a.bool = True Then
            Exit Do
        End If
    Loop

    MsgBox("test")
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM