简体   繁体   中英

Loading text asynchronously on a webforms page?

How do you click a button on a webforms page and watch the results of async requests gradually display on the page? For example, at the moment all the results appear after the postback completes!

Protected Async Sub lbtnSubmitTest_Click(sender As Object, e As EventArgs) Handles lbtnSubmitTest.Click
    Dim counter As Integer = 0
    Dim loopMax As Integer = 50
    Dim requestList As New List(Of Task(Of String))

    While counter < loopMax
        requestList.Add(FetchTokenAsync())
        counter = counter + 1
    End While

    While (requestList.Count > 0)

        Threading.Thread.Sleep(55500)

        Dim finishedTask = Await Task.WhenAny(requestList)
        requestList.Remove(finishedTask)

        Dim message = Await finishedTask
        lblResults.Text = lblResults.Text + message

    End While

    Dim file As System.IO.StreamWriter
    file = My.Computer.FileSystem.OpenTextFileWriter("C:\Temp\APITestResults.txt", True)
    file.WriteLine(lblResults.Text)
    file.Close()

End Sub

You are mixing things up.

The client (web browsers) makes a request to the server. That request might be a local synchronous or asynchronous operation, but it has no relation with the server.

Likewise, the server might execute the request synchronously or asynchronously but has no relation with the client.

They live worlds apart.

In order to asynchronously notify the client, you must use some server-to-client communication like SignalR.

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