Results 1 to 4 of 4

Thread: blocking in DoWork() while waiting for another thread to finish

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    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
    steve

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Frenzied Member MaximilianMayrhofer's Avatar
    Join Date
    Aug 2007
    Location
    IM IN YR LOOP
    Posts
    2,001

    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.

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    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.
    steve

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width