0
votes

I am currently looking at processing 2 data downloads from a WCF service that return collections of different objects and was wondering if there was a better method than running the two asynchronous tasks synchronously.

In this case can I implement as Task.WhenAll or Task.WhenAny methodology to run these tasks in parallel?

Public Property Users As IEnumerable(Of UserDto)
    Get
        Return _users
    End Get
    Set(value As IEnumerable(Of UserDto))
        _users = value
        RaisePropertyChanged("Users")
    End Set
End Property

Public Property ApplicationRoles() As IEnumerable(Of ApplicationRoleDto)
    Get
        Return _roles
    End Get
    Set(ByVal value As IEnumerable(Of ApplicationRoleDto))
        _roles = value
        RaisePropertyChanged("ApplicationRoles")
    End Set
End Property

Private Async Sub GetUserDetails()
    Users = Await _serviceHelper.GetUsers()
    ApplicationRoles = Await _serviceHelper.GetApplicationRoles
End Sub

Possible solution

I could use the Task Parrallel Library but I am not sure if this is the most efficient method, plus I can't await the return.

Parallel.Invoke(Sub() Users = _serviceHelper.GetUsers(),
                    Sub() ApplicationRoles = _serviceHelper.GetApplicationRoles())
1

1 Answers

1
votes

Task.WhenAll should work just fine. Pardon if the syntax isn't quite correct; my VB is incredibly rusty:

Private Async Function GetUserDetailsAsync() As Task
  Dim UsersTask = _serviceHelper.GetUsersAsync()
  Dim ApplicationRolesTask = _serviceHelper.GetApplicationRolesAsync
  Await Task.WhenAll(UsersTask, ApplicationRolesTask);
  Users = Await UsersTask
  ApplicationRoles = Await ApplicationRolesTask
End Function

I also took the liberty of changing your Sub to a Function (you should avoid Async Sub), and making your Async methods end with Async, as per the convention.