e.error in RunWorkerCompleted

  • Thread starter Thread starter DrLargePants
  • Start date Start date
D

DrLargePants

If I have something like below, and I catch an error in DoWork, how do
I then get e.Error in RunWorkerCompleted to populate ?


Private Sub bg_DoWork(ByVal sender As Object, ByVal e As
System.ComponentModel.DoWorkEventArgs) Handles _downloadWorker.DoWork
Try
e.Result = SomeLongRunningTask
Catch ex As Exception
e.Result = ex
End Try
End Sub

Private Sub bg_RunWorkerCompleted(ByVal sender As Object, ByVal e
As System.ComponentModel.RunWorkerCompletedEventArgs) Handles
_downloadWorker.RunWorkerCompleted

If e.Error IsNot Nothing Then
MsgBox(e.Error.Message)
Else
If e.Cancelled Then
MsgBox("Cancelled")
Else
MsgBox("Done")
End If
End If
End Sub
 
If I have something like below, and I catch an error in DoWork, how do
I then get e.Error in RunWorkerCompleted to populate ?

My reading of the documentation suggests that it gets set if an
unhandled exception occurs in DoWork. Try removing the Try/Catch
block:

"The Error property of
System.ComponentModel..::.RunWorkerCompletedEventArgs indicates that
an exception was thrown by the operation."
 
If I have something like below, and I catch an error in DoWork, how do
I then get e.Error in RunWorkerCompleted to populate ?

    Private Sub bg_DoWork(ByVal sender As Object, ByVal e As
System.ComponentModel.DoWorkEventArgs) Handles _downloadWorker.DoWork
        Try
            e.Result = SomeLongRunningTask
        Catch ex As Exception
            e.Result = ex
        End Try
    End Sub

    Private Sub bg_RunWorkerCompleted(ByVal sender As Object, ByVal e
As System.ComponentModel.RunWorkerCompletedEventArgs) Handles
_downloadWorker.RunWorkerCompleted

        If e.Error IsNot Nothing Then
            MsgBox(e.Error.Message)
        Else
            If e.Cancelled Then
                MsgBox("Cancelled")
            Else
                MsgBox("Done")
            End If
        End If
    End Sub

What error did you get? It may be related to your code execution.
 
My reading of the documentation suggests that it gets set if an
unhandled exception occurs in DoWork. Try removing the Try/Catch
block:

"The Error property of
System.ComponentModel..::.RunWorkerCompletedEventArgs indicates that
an exception was thrown by the operation."

bang on, thanks
 
Back
Top