Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 239101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:33:30+00:00 2026-05-11T20:33:30+00:00

C# 2005 I am using a background worker to process some login information. However,

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T20:33:30+00:00Added an answer on May 11, 2026 at 8:33 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 415k
  • Answers 415k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Best is subjective. You could use an SQL Lite Database… May 15, 2026 at 8:53 am
  • Editorial Team
    Editorial Team added an answer I get a StackOverflowError. If your stack trace lists a… May 15, 2026 at 8:53 am
  • Editorial Team
    Editorial Team added an answer Read up on delayed expansion in help set. By default… May 15, 2026 at 8:53 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.