How to handle Task.Run Exception

后端 未结 7 1053
离开以前
离开以前 2020-12-08 06:12

I had a problem with catching the exception from Task.Run which was resolved by changing the code as follows. I\'d like to know the difference between handling

7条回答
  •  無奈伤痛
    2020-12-08 07:02

    Building on @MennoJongerius answer the following keeps asynchronousity and moves the exception from the .wait to the Async Completed event handler:

    Public Event AsyncCompleted As AsyncCompletedEventHandler
    
    ).ContinueWith(Sub(t)
                       If t.IsFaulted Then
                           Dim evt As AsyncCompletedEventHandler = Me.AsyncCompletedEvent
                           evt?.Invoke(Me, New AsyncCompletedEventArgs(t.Exception, False, Nothing))
                       End If
                   End Sub)
    

提交回复
热议问题