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 messageand Inner exceptionCode:"Exception has been thrown by the target of an invocation."The exception usually fires when the registration success leaves scope.Code:"Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index".
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




Reply With Quote