views:

1263

answers:

1

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().

Thanks for any advice,

// Do work in background worker
//Will return less than 8 if there are no error message from the library 
        if (!this.bgwProcessLogin.CancellationPending)
        {
                // Register and wait for response
                VaxSIPUserAgentOCX.RegisterToProxy(3600);
        }
        else
        {
                // Update label
                if (this.lblRegistering.InvokeRequired)
                {
                  // do something here
                }
                else
                {
                    // Display error
                }
         }

// WAIT FOR A RESPONSE FROM THE DIAGNOTIC EVENT BEFORE CONTINUING - MAYBE JOIN HERE
        if (!this.bgwProcessLogin.CancellationPending)
        {
            if (this.responseFlag)
            {
                // Do something here   
            }
            else
            {
                // Do something else here
            }
        }


// Another function where I receive the response
private void VaxSIPUserAgentOCX_OnIncomingDiagnostic(object sender, AxVAXSIPUSERAGENTOCXLib._DVaxSIPUserAgentOCXEvents_OnIncomingDiagnosticEvent e)
    {
        string messageSip = e.msgSIP;
        //Find this message in the sip header

        string sipErrorCode = "600 User Found"; 
        if (messageSip.Contains(sipErrorCode))
        {
            // Set global flag for response
            this.responseFlag = true;
        }
}
+1  A: 

You could use a ManualResetEvent. Once your code hits the the WaitOne call, it will block until the event is Set. The WaitOne call is also overloaded so you can provide a duration to wait if you need to.

void SomeFunction()
{
// Do work in background worker
//Will return less than 8 if there are no error message from the library 
        if (!this.bgwProcessLogin.CancellationPending)
        {
                // Register and wait for response
                VaxSIPUserAgentOCX.RegisterToProxy(3600);
        }
        else
        {
                // Update label
                if (this.lblRegistering.InvokeRequired)
                {
                  // do something here
                }
                else
                {
                    // Display error
                }
         }

// WAIT FOR A RESPONSE FROM THE DIAGNOTIC EVENT BEFORE CONTINUING - MAYBE JOIN HERE

        waitEvent.WaitOne();
        if (!this.bgwProcessLogin.CancellationPending)
        {
            if (this.responseFlag)
            {
                // Do something here   
            }
            else
            {
                // Do something else here
            }
        }
}

ManualResetEvent waitEvent = new ManualResetEvent(false);

// Another function where I receive the response
private void VaxSIPUserAgentOCX_OnIncomingDiagnostic(object sender, AxVAXSIPUSERAGENTOCXLib._DVaxSIPUserAgentOCXEvents_OnIncomingDiagnosticEvent e)
    {
        string messageSip = e.msgSIP;
        //Find this message in the sip header

        string sipErrorCode = "600 User Found"; 
        if (messageSip.Contains(sipErrorCode))
        {
            // Set global flag for response
            this.responseFlag = true;
            waitEvent.Set();
        }
}
scottm