I have this code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim t = DoStuffAsync()
t.Wait()
Debug.Print(t.Result)
End Sub
Private Async Function DoStuffAsync() As Task(Of String)
Await Task.Delay(2000)
Return "Stuff"
End Function
I know this is not the right way to do it. Here is what I think is probably the right way:
Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim t = DoStuffAsync()
Debug.Print(Await t)
End Sub
Private Async Function DoStuffAsync() As Task(Of String)
Await Task.Delay(2000)
Return "Stuff"
End Function
I'm just wondering, though, why does it hang indefinitely on t.Wait()
when I run the first code example? What exactly is happening to the code execution at that point, i.e. what code is "running" when the thread is blocked on t.Wait()
?