Results 1 to 3 of 3

Thread: [RESOLVED] invocation exception using AutoResetEvent

Threaded View

  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

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