在我的DoWork()函数中,我向sip服务器注册。然后我必须等待回复。但是,我收到的回复是在另一个事件中收到的。但是,在我能够检查DoWork()中的标志之前,DoWork()已经准备就绪并且响应在之后。
我正在尝试找到一种在DoWork()中等待的方法,直到我在Diagnotic事件中得到响应。我有一个在该事件中设置的全局标志,我必须检查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;
}
}
答案 0 :(得分: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();
}
}