C#2005
我正在使用后台工作程序来处理一些登录信息。但是,后台工作程序必须停止并等待2个事件发生。完成后,后台工作人员可以完成其工作。它们是将调用AutoResetEvent的Set()方法的回调。
所以我使用AutoResetEvent设置这两个事件何时完成。但是,我似乎收到此错误消息: “调用的目标引发了异常。”
内心异常 指数超出范围。必须是非负数且小于集合的大小。参数名称:index“。
当注册成功离开范围时,通常会触发异常。
非常感谢任何建议
后台工作者的代码。
// 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();
}
}
答案 0 :(得分:1)
UpdateRegisterLabel方法中很可能存在索引错误。
从内部异常中获取堆栈跟踪,它应该指向更接近它的位置。