C#2005

我使用的背景工人来处理一些登录信息。然而,后台工作必须停止,等待2个事件的发生。一旦这些已经完成了后台工作就可以完成自己的工作。它们是回调将调用的AutoResetEvent的集()方法。

所以我使用的AutoResetEvent当这些事件2完成设置。但是,我似乎收到此错误信息: “异常已被调用的目标抛出。”

和内部异常 索引超出范围。必须为非负且小于集合的大小。参数名称:索引”

的异常通常当注册成功离开范围闪光。

非常感谢任何建议

的背景工人的代码。

// Waiting for 'Account in use' and 'Register success or failure'
AutoResetEvent[] loginWaitEvents = new AutoResetEvent[]
{
        new AutoResetEvent(false), 
        new AutoResetEvent(false)
};

private void bgwProcessLogin_DoWork(object sender, DoWorkEventArgs e)
{
      Console.WriteLine("Wait until event is set or timeout");
      loginWaitEvents[0].WaitOne(3000, true);

      if (this.accountInUseFlag)
      {
                if (this.lblRegistering.InvokeRequired)
                {
                    ///this.lblRegistering.Invoke(new UpdateRegisterLabelDelegate(this.UpdateRegisterLabel), "Account in use");
                }
                else
                {
                    ///this.lblRegistering.Text = "Account in use";
                }
                // Failed attemp
                e.Cancel = true;
                // Reset flag
                //this.accountInUseFlag = false;
                return;
       }
       else
       {
                // Report current progress
                //this.bgwProcessLogin.ReportProgress(7, "Account accepted");
       }

        Console.WriteLine("Just Wait the result of successfull login or not");
        loginWaitEvents[1].WaitOne();
        Console.WriteLine("Results for login registionSuccess: [ " + registerSuccess + " ]");

        if (this.registerSuccess)
        {
                // Report current progress
                //this.bgwProcessLogin.ReportProgress(7, "Register Succesfull");  
                // Reset flag
                //this.registerSuccess = false;
        }
        else
        {
                if (this.lblRegistering.InvokeRequired)
                {
                    //this.lblRegistering.Invoke(new UpdateRegisterLabelDelegate(this.UpdateRegisterLabel), "Failed to register");
                }
                else
                {
                   // this.lblRegistering.Text = "Failed to register";
                }
                // Failed attemp
                e.Cancel = true;               
                return;   
        }
}

// Wait for the callback to set the AutoResetEvent

// Error sometimes happens when the function leaves scope.
private void VaxSIPUserAgentOCX_OnSuccessToRegister(object sender, EventArgs e)
{
        Console.WriteLine("OnSuccessToRegister() [ Registered successfully ]");
        this.registerSuccess = true;
        this.loginWaitEvents[1].Set();
} 


// If the flag is not set, then just time out after 3 seconds for the first LoginWaitEvent.waitOne(3000, true)
 private void VaxSIPUserAgentOCX_OnIncomingDiagnostic(object sender, AxVAXSIPUSERAGENTOCXLib._DVaxSIPUserAgentOCXEvents_OnIncomingDiagnosticEvent e)
{
        string messageSip = e.msgSIP;

        //Indicates that a user is already logged on (Accout in use).
        string sipErrorCode = "600 User Found"; 
        if (messageSip.Contains(sipErrorCode))
        {
            // Set flag for account in use
            this.accountInUseFlag = true;
            Console.WriteLine("OnIncomingDiagnostic() WaitEvent.Set() accountInUseFlag: " + this.accountInUseFlag);
            loginWaitEvents[0].Set();   
        }
}
有帮助吗?

解决方案

有是最有可能在UpdateRegisterLabel方法索引错误。

从内部异常获得堆栈跟踪,它应该更紧密地指向你在哪里。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top