Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
In the following code snippet I am having difficulty waiting for the completed event to fire. The WaitOne seems to just wait forever. I need to know the proper way of doing this in VB.Net. Cheers.

VB
     Private mTable As agDBConnect.Table
    Private autoResetEvent As New System.Threading.AutoResetEvent(False)

    Public Sub GetTableCompleted(sender As Object, e As agDBConnect.GetTableCompletedEventArgs)
        Try
            Me.Dispatcher.BeginInvoke(New Action(Of agDBConnect.Table)(AddressOf FillTable), e.Result)
            autoResetEvent.Set()
        Catch ex As Exception
        End Try
    End Sub

    Private Sub FillTable(aTab As agDBConnect.Table)
        mTable = aTab
    End Sub

    Private Sub DoGetTable()
        AddHandler myagDBConnect.GetTableCompleted, AddressOf GetTableCompleted
        While myagDBConnect.State <> ServiceModel.CommunicationState.Opened
        End While
        myagDBConnect.GetTableAsync("SELECT * from s3USER")
        autoResetEvent.WaitOne()
    End Sub


  
    Private Async Sub btTestData_Click(sender As Object, e As RoutedEventArgs) Handles btTestData.Click
        Try
            Try
                mTable = Nothing
                Dim myThread As System.Threading.Thread = New Thread(AddressOf DoGetTable)
                myThread.Start()
                While myThread.ThreadState = ThreadState.Running

                End While
'>> don't want to be here until gettablecomplete has done
                If mTable IsNot Nothing AndAlso mTable.Rows.Count > 0 Then
                    txtDebug.Text = mTable.Rows(0).Columns(0).FieldName
                End If
            Catch ex As Exception
            End Try
        Catch ex As Exception
            MessageBox.Show(ex.ToString, "System Error", MessageBoxButton.OK)
        End Try

    End Sub
Posted
Updated 2-Mar-15 5:39am
v3

1 solution

Hi,
you don't need to create new Thread (and you shouldn't unless you know what are you doing); also don't need AutoResetEvent and call Dispatcher.BeginInvoke.

Just use Async with Await how it was intended to use.
You need to await the GetTableAsync method in your async click handler.
Check this article https://msdn.microsoft.com/en-us/library/hh191443.aspx[^].
 
Share this answer
 
Comments
Steveo555 3-Mar-15 3:32am    
Hi Thanks for the response, but I think you've missed the point.

The invoke is in the EVENT where it needs to be to be able to respond back to the UI thread.

The project is a Windows Phone 8.1 Silverlight application. The method GetTableAsync is a call to a web service which I can't await because it is non awaitable.

The response comes in the Completed event which is the problem. My question is "How do I await the EVENT" if I loop until something is set from it it hangs the UI thread, if I do it in the new thread (the reason for my second thread) its hangs the new thread but still does not receive the event. If I don't wait I cant use the gettableasync in a synchronous manner. Without the wait I cant use the table in the code underneath. There is a lot about this all over MSDN but the solutions don't cover the EVENT.

If someone can understand what I am trying to achieve please help.

So to clarify.

I need to wait for the EVENT to fire without hanging the application.
Matej Hlatky 4-Mar-15 3:52am    
Hi,
check this article http://www.codeproject.com/Articles/646239/NET-Asynchronous-Patterns
But I still don't understand, why you need to wait for completition. Just do your "after" work in the completed event handler.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900