2009-05-06 71 views
0

在我的DoWork()函数中,我向我们的sip服务器注册。然后我必须等待回复。但是,我收到的回复是在另一个事件中收到的。但是,在我能够检查DoWork()中的标志之前,DoWork()已经完成并且响应已经完成。C#加入后台工作线程中的线程DoWork()

我试图找到一种方法,在DoWork()中等待,直到我在Diagnosis事件中得到响应。我有一个全局标志,在那个事件中我必须检查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)什么是响应标志?它在哪里设置为真? (2)你可以发布你实际使用后台工作的方法吗? – Grzenio 2009-05-06 14:42:34

+0

你好。响应是'600用户找到'sip协议。如果它已被设置为true,则找到用户。我确实改变了代码,使它看起来更简单。我认为斯科蒂有最好的解决方案。我现在就这样做。完成后我会回复。谢谢。 – ant2009 2009-05-06 16:57:56

回答

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(); 
     } 
}