Threading with manualResetEvent
Hello,
I have the following code in a class library. And I wait for a call back into my main application. I am making a DownloadStringAsync call so I have to wait a few seconds to get the callback after it has finished. I have a 3 of these calls to wait for (but for being simple just testing with one), so in my main application I am using ManualResetEvent to wait all of them to finish. So I will block until they have been set in the callback functions.
However, after testing the callback don't get called. I am thinking when the code gets blocked by the ManualResetEvent as its blocking the DownloadStringAsync. As when I comment out this code everything works fine.
So I think as soon as I make a call to: objNoGateway.NoGatewayStatus(sipUsername, statusDisplay1.PhoneNumber); And when the code reaches here: handle.WaitOne(); It will block the code in the class library.
I think the main UI thread is being blocked, so cannot receive any messages.
Many thanks for any suggestions,
My class library:
Code:
' Event handler that makes a call back in my main application
' Event handler and method that handles the event
Public NoGatewayCompletedEvent As EventHandler(Of NoGatewayEventArgs)
' The method that raises the event.
Private Sub OnNoGatewayCompleted(ByVal sender As Object, ByVal e As NoGatewayEventArgs)
If NoGatewayCompletedEvent IsNot Nothing Then
NoGatewayCompletedEvent(Me, e)
End If
End Sub
' Start the Async call to find if NoGateway is true or false
Public Sub NoGatewayStatus(ByVal sipUsername As String, ByVal phoneNumber As String)
Dim strURL As String = String.Format("http://xxxxxxxxxxxxxxx={0}&CalledNumber={1}", sipUsername, phoneNumber)
If (Not wc.IsBusy) Then
Try
Dim errorMsg As String = String.Empty
wc.DownloadStringAsync(New Uri(strURL))
Catch ex As WebException
Console.WriteLine("IsNoGateway: " & ex.Message)
Catch ex As Exception
Console.WriteLine("IsNoGateway: " & ex.Message)
End Try
Else
Console.WriteLine("WebClient: IsNoGateWay(): Busy please try again")
End If
End Sub
Private Sub wc_DownloadStringCompleted(ByVal sender As Object, ByVal e As DownloadStringCompletedEventArgs)
If e.Error Is Nothing Then
If e.Result = "No gateway" Then
OnNoGatewayCompleted(Me, New NoGatewayEventArgs(validateResponse_e.VALIDATION_FAILED))
Console.WriteLine("NoGatway() DownloadedCompleted: " & e.Result)
Else
OnNoGatewayCompleted(Me, New NoGatewayEventArgs(validateResponse_e.OK))
Console.WriteLine("NoGateway() DownloadCompleted: " & e.Result)
End If
Else
Me.OnNoGatewayCompleted(Me, New NoGatewayEventArgs(validateResponse_e.SERVER_FAILED))
Console.WriteLine("No Gateway: DownloadCompleted() Error: " & e.Error.Message)
End If
End Sub
In my main application I register this callback. And wait for the for the result. Then set the ManualResetEvent.
Code:
Private waitValidateCallResponse() As ManualResetEvent = { New ManualResetEvent(False), New ManualResetEvent(False), New ManualResetEvent(False) }
' Event handler for NoGateway event
Private Sub OnNoGatewayCompleted(ByVal sender As Object, ByVal e As NoGatewayEventArgs)
Console.WriteLine("OnNoGatewayComleted: " & e.noGateway)
waitValidateCallResponse(0).Set()
End Sub
Where I am start and then wait for the thread to finish
Code:
Private Sub ValidateDialedNumber()
'Incorrect country code has been entered.
Using objNoGateway As New NoGateway()
AddHandler objNoGateway.NoGatewayCompletedEvent, AddressOf OnNoGatewayCompleted
objNoGateway.NoGatewayStatus(sipUsername, statusDisplay1.PhoneNumber)
End Using
' Block here - Wait for all reponses to finish before moving on
'WaitHandle.WaitAll(waitValidateCallResponse);
waitEvent.WaitOne(5000, True)
Console.WriteLine("All thread finished")
End Sub