Results 1 to 3 of 3

Thread: [RESOLVED] invocation exception using AutoResetEvent

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    Resolved [RESOLVED] invocation exception using AutoResetEvent

    Hello,

    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 callback 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
    Code:
    "Exception has been thrown by the target of an invocation."
    and Inner exception
    Code:
    "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.
    Code:
    ' Waiting for 'Account in use' and 'Register success or failure'
    Private loginWaitEvents() As AutoResetEvent = { New AutoResetEvent(False), New AutoResetEvent(False) }
    
    Private Sub bgwProcessLogin_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs)
    		  Console.WriteLine("Wait until event is set or timeout")
    		  loginWaitEvents(0).WaitOne(3000, True)
    
    		  If Me.accountInUseFlag Then
    					If Me.lblRegistering.InvokeRequired Then
    						Me.lblRegistering.Invoke(New UpdateRegisterLabelDelegate(Me.UpdateRegisterLabel), "Account in use")
    					Else
    						Me.lblRegistering.Text = "Account in use"
    					End If
    					' Failed attemp
    					e.Cancel = True
    					' Reset flag
    					Me.accountInUseFlag = False
    					Return
    		   Else
    					' Report current progress
    					Me.bgwProcessLogin.ReportProgress(7, "Account accepted")
    		   End If
    
    			Console.WriteLine("Just Wait the result of successfull login or not")
    			loginWaitEvents(1).WaitOne()
    
    			If Me.registerSuccess Then
    					' Report current progress
    					Me.bgwProcessLogin.ReportProgress(7, "Register Succesfull")
    					' Reset flag
    					Me.registerSuccess = False
    			Else
    					If Me.lblRegistering.InvokeRequired Then
    						Me.lblRegistering.Invoke(New UpdateRegisterLabelDelegate(Me.UpdateRegisterLabel), "Failed to register")
    					Else
    						Me.lblRegistering.Text = "Failed to register"
    					End If
    					' Failed attemp
    					e.Cancel = True
    					Return
    			End If
    End Sub
    
    ' Wait for the callback to set the AutoResetEvent
    
    ' Error sometimes happens when the function leaves scope.
    Private Sub VaxSIPUserAgentOCX_OnSuccessToRegister(ByVal sender As Object, ByVal e As EventArgs)
    			Console.WriteLine("OnSuccessToRegister() [ Registered successfully ]")
    			Me.registerSuccess = True
    			Me.loginWaitEvents(1).Set()
    End Sub
    
    
    ' If the flag is not set, then just time out after 3 seconds for the first LoginWaitEvent.waitOne(3000, true)
     Private Sub VaxSIPUserAgentOCX_OnIncomingDiagnostic(ByVal sender As Object, ByVal e As AxVAXSIPUSERAGENTOCXLib._DVaxSIPUserAgentOCXEvents_OnIncomingDiagnosticEvent)
    			Dim messageSip As String = e.msgSIP
    
    			'Indicates that a user is already logged on (Accout in use).
    			Dim sipErrorCode As String = "600 User Found"
    			If messageSip.Contains(sipErrorCode) Then
    				' Set flag for account in use
    				Me.accountInUseFlag = True
    				Console.WriteLine("OnIncomingDiagnostic() WaitEvent.Set() accountInUseFlag: " & Me.accountInUseFlag)
    				loginWaitEvents(0).Set()
    			End If
     End Sub
    Last edited by steve_rm; May 16th, 2009 at 02:31 PM.
    steve

  2. #2

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    Re: invocation exception using AutoResetEvent

    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
    steve

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: invocation exception using AutoResetEvent

    You have a section of code like this:
    vb Code:
    1. If Me.accountInUseFlag Then
    2.     '...
    It's quite possible that that thread might read the value of the variable and then another thread could change the value while the contents of the If block is being executed. Would that be a problem in the context of your application? If so then you should make that code a critical section, otherwise there's no point.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width