Hello,

I am using the code below to login to a softphone. However, the login progess is a long process as there are many things that have to be initialized and checks to be made, I have only put a few on here, as it would make the code to long to post.

In the code below I am checking if the CancellationPending if the CancelAsync has been called in my cancel button click event, before doing each check. Is this correct? Also if the check fails I also call the CancelAsync and set the e.Cancel to true.

I would like to know if my method I have used here is the best method to use.

Many thanks for any advice,

Code:
Private Sub bgwProcessLogin_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs)
'        
'         * Perform at test to see if the background worker has been
'         * cancelled by the user before attemping to continue to login.
'         * 
'         * Cancel background worker on any failed attemp to login
'         

		' Start with cancel being false as to reset this if cancel has been set to true
		' in the cancel button.
		e.Cancel = False

		Dim connection_test As New NetworkingTest()
		If (Not Me.bgwProcessLogin.CancellationPending) Then
			' Check local LAN or Wireless connection               
			If (Not connection_test.IsNetworkConnected()) Then
				' Update label
				If Me.lblRegistering.InvokeRequired Then
					Me.lblRegistering.Invoke(New UpdateRegisterLabelDelegate(UpdateRegisterLabel), "No network connection")
				Else
					Me.lblRegistering.Text = "No network connection"
				End If
				' Failed attemp
				Me.bgwProcessLogin.CancelAsync()
				e.Cancel = True
				Return
			End If
			' Report current progress
			Me.bgwProcessLogin.ReportProgress(0, "Network connected")
		Else
			' User cancelled 
			e.Cancel = True
			Return
		End If

		' Test if access to Server is available
		If (Not Me.bgwProcessLogin.CancellationPending) Then
			If (Not connection_test.IsSIPServerAvailable()) Then
				' Update label
				If Me.lblRegistering.InvokeRequired Then
					Me.lblRegistering.Invoke(New UpdateRegisterLabelDelegate(UpdateRegisterLabel), "Server unavailable")
				Else
					Me.lblRegistering.Text = "Server unavailable"
				End If
				' Failed attemp
				Me.bgwProcessLogin.CancelAsync()
				e.Cancel = True
				Return
			End If
			' Report current progress
			Me.bgwProcessLogin.ReportProgress(1, "Server available")
		Else
			' User cancelled 
			e.Cancel = True
			Return
		End If
	        .
                .
                .
End Sub

 Private Sub bgwProcessLogin_RunWorkerCompleted(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs)
		' Check for any errors
		If e.Error Is Nothing Then
			If e.Cancelled Then
				' User cancelled login or login failed                
			Else
				' Login completed successfully                
			End If
		Else
			' Something failed display error
			Me.statusDisplay1.CallStatus = e.Error.Message
		End If
 End Sub

Private Sub bgwProcessLogin_ProgressChanged(ByVal sender As Object, ByVal e As ProgressChangedEventArgs)
		Me.lblRegistering.Text = e.UserState.ToString()
End Sub

Private Sub btnCancel_Click(ByVal sender As Object, ByVal e As EventArgs)
		' Cancel the logging in process
		Me.bgwProcessLogin.CancelAsync()
		Me.lblRegistering.Text = "Logged out"
End Sub