Results 1 to 2 of 2

Thread: [RESOLVED] Cancelling progress Background worker

  1. #1

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

    Resolved [RESOLVED] Cancelling progress Background worker

    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
    steve

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

    Re: Cancelling progress Background worker

    That's pretty much it except there's no point calling CancelAsync in the DoWork event handler. All calling CancelAsync does is set CancellationPending to True. CancelAsync is only useful when called from the UI thread in order to signal to the secondary thread that the operation should be cancelled, in which case e.Cancel is set to True and you return. If you're already in the secondary thread and you want to cancel then you would simply set e.Cancel to True and return, which you're already doing. Calling CancelAsync is redundant.
    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