How to take the result of a method called Asynchronously?
to not block the UI of my applications when there is a long operation I use to do:
Public Sub ButtonHandler() handles Button
Dim auxDelegate as Action
auxDelegate = New Action(AddressOf DoAction)
auxDelegate.BeginInvoke(AddressOf EndAction,Nothing)
End Sub
Private Sub DoAction()
''# Do long time operations here
End Sub
Private Sub EndAction()
''# Update results here开发者_高级运维
End Sub
Is there a way to take the result of DoAction in case that it is a Function instead of a Sub and use that result at EndAction other that defining a class attribute and put the value there?
For example, if DoAction is like this:
Private Function DoAction() As Boolean
return True
End Sub
There is a way of taking the result of DoAction at EndAction?
Thanks in advance!
Action is a pointer to a function that takes no argument and returns no value. If you want your function to return some value you need to use a different delegate. For example you could use Func(Of Boolean) if you want your function to return a boolean value:
Dim auxDelegate As Func(Of Boolean)
Public Sub ButtonHandler() handles Button
auxDelegate = AddressOf DoAction
auxDelegate.BeginInvoke(AddressOf EndAction, Nothing)
End Sub
Private Function DoAction() As Boolean
Return True
End Function
Private Sub EndAction(ByVal ar As IAsyncResult)
' call EndInvoke on the delegate to obtain the result
Dim result As Boolean = auxDelegate.EndInvoke(ar)
End Sub
精彩评论