My problem: I want to use TPL in WinForms application under .NET 4 and I need the task continuations to elevate any unhandled exceptions immediately ("fast throw") instead of waiting for GC collecting the Task. Is it possible?
In .NET 4.5 with async/await support it is possible to write:
Public Class AwaitForm
Inherits Form
Private Async Sub Execute()
Dim uiScheduler = TaskScheduler.FromCurrentSynchronizationContext()
Try
Await Me.LongWork().
ContinueWith(Sub(t) Me.LongWorkCompleted(), uiScheduler)
Catch ex As Exception
' yay, possible to handle here
' eg. MsgBox(ex.Message)
Throw
End Try
End Sub
Private Async Function LongWork() As Task
Await Task.Delay(1000)
End Function
Private Sub LongWorkCompleted()
Throw New Exception("Ups")
End Sub
End Class
The exception in continuation would be thrown immediately if not handled in Excecute method.
How to achieve same behavior in .NET 4 without async/await support?