e.error in RunWorkerCompleted

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
 
J

Jack Jackson

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."
 
K

kimiraikkonen

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.
 
D

DrLargePants

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top