简单的 ASP.NET 4.5 异步和等待 VB.NET

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14996270/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 12:26:49  来源:igfitidea点击:

Simple ASP.NET 4.5 async and await in VB.NET

asp.netvb.netasynchronousasync-await

提问by sqlchris

I am trying to get a simple example of async with await working but I don't think it is. I am thinking this code should take 10 seconds (or just over 10 seconds) to run as each function within the for each loop is supposed to run asynchronously.

我正在尝试使用 await 来获取一个简单的异步示例,但我认为不是。我认为这段代码应该需要 10 秒(或刚刚超过 10 秒)来运行,因为 for each 循环中的每个函数都应该异步运行。

This is an asp.net web forms and Async="true" is present in the page declaration.

这是一个 asp.net 网络表单,页面声明中存在 Async="true"。

Inherits System.Web.UI.Page

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

    'This needs to execute different asynchronous processes based on the array value
    Dim ItemList(2) As String

    ItemList(0) = "A"
    ItemList(1) = "B"
    ItemList(2) = "C"

    Dim time_start As DateTime
    Dim time_end As DateTime

    Dim r1 As String
    Dim r2 As String
    Dim r3 As String

    'capture start time
    time_start = DateTime.Now

    'run async processes for each item in array
    For Each element As String In ItemList
        Select Case element
            Case "A"
                r1 = Await processAsyncA(10) & "  "
            Case "B"
                r2 = Await processAsyncB(10) & "  "
            Case "C"
                r3 = Await processAsyncC(10) & "  "
        End Select
    Next

    'capture end time
    time_end = DateTime.Now

    'display total duration in seconds
    Label1.Text = DateDiff(DateInterval.Second, time_start, time_end)

End Sub

Protected Async Function processAsyncA(ByVal waittime As Integer) As Task(Of String)
    Await Task.Delay(waittime * 1000)
    Return waittime.ToString
End Function

Protected Async Function processAsyncB(ByVal waittime As Integer) As Task(Of String)
    Await Task.Delay(waittime * 1000)
    Return waittime.ToString
End Function

Protected Async Function processAsyncC(ByVal waittime As Integer) As Task(Of String)
    Await Task.Delay(waittime * 1000)
    Return waittime.ToString
End Function

Thanks in advance!

提前致谢!

回答by Damien_The_Unbeliever

No, they won't run asynchronously because you've said "don't carry on until you've got the result" here:

不,它们不会异步运行,因为您在这里说过“在得到结果之前不要继续”:

r1 = Await processAsyncA(10)

What you should instead do is launch all of the processXXXfunctions and thenawait all of them. Something like:

您应该做的是启动所有processXXX功能,然后等待所有功能。就像是:

Dim l as New List(Of Task(Of String))

'run async processes for each item in array
For Each element As String In ItemList
    Select Case element
        Case "A"
            l.Add(processAsyncA(10))
        Case "B"
            l.Add(processAsyncB(10))
        Case "C"
            l.Add(processAsyncC(10))
    End Select
Next

r1 = Await l(0) & "  "
r2 = Await l(1) & "  "
r3 = Await l(2) & "  "

(Not the cleanest code, but hopefully you get the gist)

(不是最干净的代码,但希望你能明白要点)