Click here to Skip to main content
15,907,392 members
Home / Discussions / C#
   

C#

 
GeneralRe: An error witrh SQL Server and C# Pin
Mohammad Dayyan16-May-09 23:34
Mohammad Dayyan16-May-09 23:34 
GeneralRe: An error witrh SQL Server and C# Pin
Guffa22-May-09 13:20
Guffa22-May-09 13:20 
QuestionSentence Scanning Pin
beibay16-May-09 11:06
beibay16-May-09 11:06 
AnswerRe: Sentence Scanning Pin
Mycroft Holmes16-May-09 20:52
professionalMycroft Holmes16-May-09 20:52 
GeneralRe: Sentence Scanning Pin
beibay16-May-09 21:11
beibay16-May-09 21:11 
QuestionPlay Video stream coming from a network stream Pin
dihaz16-May-09 10:19
dihaz16-May-09 10:19 
QuestionDataset Update Pin
Stefano Negro16-May-09 10:03
Stefano Negro16-May-09 10:03 
QuestionMulti-threading using AutoResetEvent [modified] Pin
steve_rm16-May-09 9:28
steve_rm16-May-09 9:28 
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,


// 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();
           
            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();   
            }
}


modified on Saturday, May 16, 2009 10:18 PM

AnswerRe: Multi-threading using AutoResetEvent Pin
S. Senthil Kumar16-May-09 9:39
S. Senthil Kumar16-May-09 9:39 
AnswerRe: Multi-threading using AutoResetEvent Pin
steve_rm16-May-09 17:24
steve_rm16-May-09 17:24 
GeneralRe: Multi-threading using AutoResetEvent Pin
Joe Woodbury16-May-09 18:21
professionalJoe Woodbury16-May-09 18:21 
GeneralRe: Multi-threading using AutoResetEvent Pin
steve_rm16-May-09 23:53
steve_rm16-May-09 23:53 
GeneralRe: Multi-threading using AutoResetEvent Pin
S. Senthil Kumar17-May-09 1:26
S. Senthil Kumar17-May-09 1:26 
GeneralRe: Multi-threading using AutoResetEvent Pin
Daniel Grunwald17-May-09 1:30
Daniel Grunwald17-May-09 1:30 
AnswerRe: Multi-threading using AutoResetEvent Pin
Luc Pattyn16-May-09 10:00
sitebuilderLuc Pattyn16-May-09 10:00 
Questionfind file in web Pin
michaelgr116-May-09 4:17
michaelgr116-May-09 4:17 
AnswerRe: find file in web Pin
Roland Szigeti16-May-09 4:23
Roland Szigeti16-May-09 4:23 
GeneralRe: find file in web Pin
michaelgr116-May-09 4:32
michaelgr116-May-09 4:32 
GeneralRe: find file in web Pin
Roland Szigeti16-May-09 4:34
Roland Szigeti16-May-09 4:34 
GeneralRe: find file in web Pin
michaelgr116-May-09 4:36
michaelgr116-May-09 4:36 
GeneralRe: find file in web Pin
harold aptroot16-May-09 6:16
harold aptroot16-May-09 6:16 
GeneralRe: find file in web Pin
Manas Bhardwaj16-May-09 6:17
professionalManas Bhardwaj16-May-09 6:17 
GeneralRe: find file in web Pin
michaelgr116-May-09 6:35
michaelgr116-May-09 6:35 
AnswerRe: find file in web Pin
MumbleB16-May-09 6:45
MumbleB16-May-09 6:45 
GeneralRe: find file in web Pin
michaelgr116-May-09 7:09
michaelgr116-May-09 7:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.