blocking in DoWork() while waiting for another thread to finish
Hello,
In my DoWork() function I register with our sip server. Then I have to wait for a response back. However, the response I get is received in another event. However, before I am able to check the flag in the DoWork() the DoWork() has all ready finished and the response comes after.
I am trying to find a way to wait in the DoWork() until I get a response in the Diagnotic event. I have a global flag that is set in that event that I have to check in the DoWork().
I was thinking of maybe blocking in the DoWork() until the other event is finished. Maybe using a join. However, I am not sure that this is the best solution.
Thanks for any advice,
Code:
' Do work in background worker
'Will return less than 8 if there are no error message from the library
If (Not Me.bgwProcessLogin.CancellationPending) Then
' Register and wait for response
VaxSIPUserAgentOCX.RegisterToProxy(3600)
Else
' Update label
If Me.lblRegistering.InvokeRequired Then
' do something here
Else
' Display error
End If
End If
' WAIT FOR A RESPONSE FROM THE DIAGNOTIC EVENT BEFORE CONTINUING - MAYBE JOIN HERE
If (Not Me.bgwProcessLogin.CancellationPending) Then
If Me.responseFlag Then
' Do something here
Else
' Do something else here
End If
End If
' Another function where I receive the response
private void VaxSIPUserAgentOCX_OnIncomingDiagnostic(Object sender, AxVAXSIPUSERAGENTOCXLib._DVaxSIPUserAgentOCXEvents_OnIncomingDiagnosticEvent e)
Dim messageSip As String = e.msgSIP
'Find this message in the sip header
Dim sipErrorCode As String = "600 User Found"
If messageSip.Contains(sipErrorCode) Then
' Set global flag for response
Me.responseFlag = True
End If
Re: blocking in DoWork() while waiting for another thread to finish
I've never actually had to do this but my guess is that you would have to use some sort of lock object to halt the BackgroundWorker, then signal it from the other event handler so it can continue. Hopefully you or someone else will come up with a solution first but, if not, I should have time to take a look at this either at lunch time today (in about an hour) or after work.
Re: blocking in DoWork() while waiting for another thread to finish
I suggest using Thread.SpinWait() in this particular case within a do loop to check if the response flag has arrived.
Re: blocking in DoWork() while waiting for another thread to finish
Hello,
I looked at the Thread.SpinWait. I don't really want to loop as I don't know really how long I have to wait for the another function to finish.
However, I have been looking at the following: AutoResetEvent. That blocks the thread until an event has been signaled.
http://msdn.microsoft.com/en-us/libr...esetevent.aspx
I also found this manualReset:
http://msdn.microsoft.com/en-us/libr...esetevent.aspx
Not sure which one would be better.