BackGroundWorker and the RunWorkerCompletedEventArgs

I'm using the BackGroundWorker to do some file copying, and everything seems to workexceptnone of the arguments in RunWorkerCompletedEventArgs are getting set. If I cancel the operation, the worker.CancelationPending property is set to true, so I can detect the cancelation while I'm in the midst of copying files and I can back out of the operation, but when I get the RunWorkerCompleted event, the arguments do not indicate that the operation was canceled. I could define a global boolean for when the Cancel button is pushed, but I wonder if I could be missing any error messages in the EventArgs.

Am I missing something about how the BackGroundWorker functions, or is there some property for the BackGroundWorker that needs to set?

I've created the simple test routine below to test the basic functions of the BackGroundWorker with a simple loop to use up time within the background thread. The form has two buttons (Start & Cancel), a TextBox, and a ProgressBar

I'm running Visual Basic Express / Microsoft Visual Studio 2005 / Version 8.0.50727.42 (RTM.050727-4200)

Any suggestions will be appreciated

Imports System.ComponentModel
Public Class
Form1
Private Sub Button_Start_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Start.Click
Me.TextBox1.Text = Nothing
Me.BGW.RunWorkerAsync(10)
End Sub

Private Sub Button_Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Cancel.Click
Me.BGW.CancelAsync()
End Sub

Private Sub BGW_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs) Handles BGW.DoWork
Dim worker As BackgroundWorker = CType(sender, BackgroundWorker)
DoSomeWork(worker, e)
End Sub

Private Sub DoSomeWork(ByVal worker As BackgroundWorker, ByVal e As DoWorkEventArgs)
Dim i, x As Integer
Dim Status As String
For i = 0 To 100000000
If worker.CancellationPending = True Then
Exit For 'If e.Cancelled won't work, maybe set a Cancel Boolean?
Else 'Just loop to use some time
x = 123 * 456
If i Mod 10000000 = 0 Then 'Send an occasional progress report
Status = CType(i / 10000000, String)
worker.ReportProgress(i / 10000000 * 10, Status)
End If
End If
Next
End Sub

Private Sub BGW_ProgressChanged(ByVal sender As Object, ByVal e As ProgressChangedEventArgs) Handles BGW.ProgressChanged
Me.ProgressBar1.Value = e.ProgressPercentage
Me.TextBox1.Text = CType(e.UserState, String)
End Sub

Private Sub BGW_RunWorkerCompleted(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs) Handles BGW.RunWorkerCompleted
If (e.Error IsNot Nothing) Then
MessageBox.Show(e.Error.Message)
ElseIf e.Cancelled Then
Me.TextBox1.Text = "Canceled"
Else
Me.TextBox1.Text = "Done"
End If
End Sub

End Class

[3167 byte] By [RichKorea] at [2007-12-24]
# 1

Add the line in red

If worker.CancellationPending = True Then

e.Cancel = True

TallDude at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 2
Thanks - I figured it was something simiple
RichKorea at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Language...