Call Exception Using AutoResetEvent

C # 2005

I am using a background worker to process some login details. However, the background worker must stop and wait for 2 events to happen. Once they run out, the background worker can do his job. These are callbacks that will call the Set () method on the AutoResetEvent.

So I am using AutoResetEvent to set when these 2 events are complete. However, it looks like I got an error: "Exception was thrown by the target of the call."

And the internal exception Index was out of touch. Must be non-negative and less than the size of the collection. Parameter name: index ".

An exception usually occurs when the success of the registration leaves the area.

Thanks a lot for any advice

Code for the working background.

// 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


a source to share


1 answer


The UpdateRegisterLabel method most likely has an indexing error.



Get the stack trace from the inner exception, it should point you closer to where it is.

+1


a source







All Articles