Question

Dans ma fonction DoWork (), je m'enregistre auprès de notre serveur SIP. Ensuite, je dois attendre une réponse. Cependant, la réponse que je reçois est reçue dans un autre événement. Cependant, avant que je puisse vérifier l'indicateur dans DoWork (), DoWork () est prêt et la réponse vient après.

J'essaie de trouver un moyen d'attendre dans DoWork () jusqu'à ce que je reçoive une réponse dans l'événement Diagnotic. J'ai un indicateur global défini dans cet événement que je dois vérifier dans DoWork ().

Merci pour vos conseils,

// 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;
        }
}
Était-ce utile?

La solution

Vous pouvez utiliser un ManualResetEvent . Une fois que votre code a reçu l'appel WaitOne, il sera bloqué jusqu'à ce que l'événement soit défini. L’appel WaitOne est également surchargé, vous pouvez donc lui attribuer une durée d’attente si vous en avez besoin.

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();
        }
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top