C # attaching threads in a background working document DoWork ()
In my DoWork () function, I register with our sip server. Then I have to wait for an answer. However, the answer I get is obtained in a different case. However, before I can check the flag in DoWork (), DoWork () has everything ready to complete and the response comes after.
I am trying to find a way to wait in DoWork () until I get a diagnosis response. I have a global flag set in this case that I should check 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;
}
}
0
a source to share
1 answer
You can use ManualResetEvent . Once your code hits the WaitOne call, it will block until the event is set. The WaitOne call is also overloaded, so you can specify the length of time to wait if you want.
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();
}
}
+1
a source to share