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 ?
WatersideAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
Please post your code of the constructor that is calling the asnyc method.
WatersideAuthor Commented:
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

Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
You could declare a Custom Event that gets raised by your Class:
Public Class getGeo

    Public Event DictionaryFilled(ByVal sender As getGeo)

End Class

Open in new window


You'd need to separate out, however, the starting of the search process from the Constructor so that you can actually subscribe to the event:
Public Class getGeo

    Public Event DictionaryFilled(ByVal sender As getGeo)

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

    Public Sub StartSearch()
        goSearch()
    End Sub

End Class

Open in new window


When you create your instance of getGeo, use AddHandler to wire up the new custom event:
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim geo As New getGeo(someList)
        AddHandler geo.DictionaryFilled, AddressOf geo_DictionaryFilled
        geo.StartSearch()
    End Sub

    Public Sub geo_DictionaryFilled(ByVal sender As getGeo)
        ' ... possibly do something with "sender" ...
        For Each something in sender.Results

        Next
    End Sub

Open in new window


Finally, raise that custom event when you're all done:
    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
            RaiseEvent DictionaryFilled(Me)
        End If
    End Sub

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
WatersideAuthor Commented:
thanks
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.