views:

232

answers:

1

Hello,

C# 2005

I am using a background worker to process some login information. However, the background worker has to stop and wait for 2 events to happen. Once these have finished the background worker can complete its job. They are callbacks that will call the Set() method of the AutoResetEvent.

So I am using AutoResetEvent to set when these 2 events have finished. However, I seemed to be getting this error message: "Exception has been thrown by the target of an invocation."

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

The exception usually fires when the registration success leaves scope.

Many thanks for any advice

The code for the background worker.

// 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();   
        }
}
+1  A: 

There is most likely an indexing error in the UpdateRegisterLabel method.

Get a Stack Trace from the inner exception, it should point you more closely to where it is.

Adam Ruth
Hello, I solved my problem. It was something in the RunWorkercompleted. However, there is one more thing. The registerSuccess and AccountInUse are global because they are been accessed from 2 different threads. Would it be better to put a lock on them? Many thanks
robUK
You don't need to lock bools, but you may want to mark them as volatile to ensure that each thread always has the most up-to-date value.
Adam Ruth