0

在我的 DoWork() 函数中,我向我们的 sip 服务器注册。然后我必须等待回复。但是,我得到的响应是在另一个事件中收到的。但是,在我能够检查 DoWork() 中的标志之前,DoWork() 已经准备就绪,并且响应随之而来。

我试图找到一种方法来等待 DoWork() 直到我在诊断事件中得到响应。我有一个在该事件中设置的全局标志,我必须检查 DoWork()。

感谢您的任何建议,

// 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;
        }
}
4

1 回答 1

1

您可以使用ManualResetEvent。一旦您的代码遇到 WaitOne 调用,它将阻塞,直到事件被设置。WaitOne 调用也超载,因此您可以在需要时提供等待的持续时间。

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();
        }
}
于 2009-05-06T14:41:57.720 回答