Link to home
Start Free TrialLog in
Avatar of Waterside
Waterside

asked on

Get notification from a Class that runs Async code

Hello

As the title says...

My class takes a List(of String) in the constructor and calls an Async method with a Completed handler.  The Completed handler adds the result of the Async to a dictionary object and then calls the method again for the next item in the List.

How can I notify the code that calls the class that the Dictionary filling process is now complete ?
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Please post your code of the constructor that is calling the asnyc method.
Avatar of Waterside
Waterside

ASKER

Public Class getGeo
        Private lstProps As New List(Of String)
        Private intPropNo As Integer = 0
        Private dictResults As New Dictionary(Of String, GeoCoordinate)

        Public Property Results As Dictionary(Of String, GeoCoordinate)
            Get
                Return dictResults
            End Get
            Set(value As Dictionary(Of String, GeoCoordinate))
                dictResults = value
            End Set
        End Property

        Public Sub New(propsIn As List(Of String))
            lstProps = propsIn
            goSearch()
        End Sub

        Private Sub goSearch()
            If intPropNo < lstProps.Count Then
                Dim gq = New GeocodeQuery()
                gq.GeoCoordinate = New GeoCoordinate(54.06579, -1.928362)
                gq.SearchTerm = lstProps(intPropNo)
                AddHandler gq.QueryCompleted, AddressOf gq_QueryCompleted
                gq.QueryAsync()
            Else
                Results = dictResults
            End If
        End Sub

        Private Sub gq_QueryCompleted(sender As Object, e As QueryCompletedEventArgs(Of IList(Of MapLocation)))
            If e.[Error] Is Nothing Then
                If e.Result.Count > 0 Then
                    Dim foundAddress As String = getAddressString(e.Result(0).Information)
                    If lstProps(intPropNo).StartsWith(foundAddress, StringComparison.CurrentCultureIgnoreCase) Then
                        dictResults.Add(foundAddress, e.Result(0).GeoCoordinate)
                        dBug(foundAddress & ", " & e.Result(0).GeoCoordinate.Latitude & ", " & e.Result(0).GeoCoordinate.Longitude)
                        'Temp
                        locs.Add(foundAddress, e.Result(0).GeoCoordinate)
                    Else
                        'tb.Text = lstProps(intPropNo) & " @ " & "Not Found"
                    End If
                    intPropNo += 1
                End If
            End If
            goSearch()
        End Sub

    End Class

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks