autoresetevent를 사용한 호출 예외
-
22-08-2019 - |
문제
C# 2005
배경 작업자를 사용하여 로그인 정보를 처리하고 있습니다. 그러나 배경 작업자는 2 개의 이벤트가 발생할 때까지 멈추고 기다려야합니다. 이것들이 끝나면 배경 노동자는 작업을 완료 할 수 있습니다. 그들은 autoresetevent의 set () 메소드를 호출하는 콜백입니다.
따라서이 두 이벤트가 완료되었을 때 Autoresetevent를 사용하여 설정하고 있습니다. 그러나이 오류 메시지를 받고있는 것 같았습니다. "호출 대상에 의해 예외가 발생했습니다."
내부 예외 지수는 범위를 벗어났습니다. 음수가 아닌 크기보다 적어야합니다. 매개 변수 이름 : 색인 ".
등록 성공이 범위를 떠날 때 일반적으로 예외가 발생합니다.
조언에 감사드립니다
배경 작업자의 코드.
// 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 메소드에는 인덱싱 오류가있을 가능성이 높습니다.
내부 예외에서 스택 추적을 가져 오면 어디에 있는지 더 가깝게 지적해야합니다.
제휴하지 않습니다 StackOverflow