First of all I searched here and have seen many similar questions but none of them is what I want.
I've a function that takes some time to return a value To simplify, let's say it's:
Private Function longProcess() As Boolean
Threading.Thread.Sleep(10000)
Return True
End Function
I want to run it and get its value, let's say on clicking Button1
I tried the following code and it's working perfectly
Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'some stuff
Dim output As Boolean = Await Task.Run(Of Boolean)(Function() longProcess())
'continue another stuff when the longProcess completes
End Sub
If this way is good enough ? if not, What problems it may have ?
I was using another way but it was making CPU usage higher because of Application.DoEvents()
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'some stuff
Dim awaiter = Task.Run(Of Boolean)(Function() longProcess()).GetAwaiter()
Do While Not awaiter.IsCompleted
Application.DoEvents()
Loop
Dim output As Boolean = awaiter.GetResult()
'continue another stuff when the longProcess completes
End Sub