Kallol Kallol - 1 year ago 125
Vb.net Question

Task.Run - Handling exceptions

I am having a function as below (kept only relevant code for this example):

Public Async Function RefreshCompanyCodesAsync() As Task(Of Boolean)
For ctr=1 to 2000
Await Task.Delay(1000).ConfigureAwait(False)
If ctr=5 Then Throw New ApplicationException("Why 5?")
return true
End Function

As you can see, I am generating an exception when ctr = 5 just to check how the exception will be handled in the UI.

I then call this function inside a button click

Dim task1 = Task.Run(Sub()
End Sub)
Dim cTask = task1.ContinueWith(Sub(antecedent)
If task1.Status = TaskStatus.RanToCompletion Then
MsgBox("All done")
ElseIf task1.Status = TaskStatus.Faulted Then
End If
End Sub)

The task runs to completion instead of being faulted. I am guessing thats because a wrapper task runs to completion instead of the original task that threw an exception. However, am not sure how to get to the original task and handle the exception.

I have tried with

Await RefreshCompanyCodesAsync()
Catch ax As AggregateException
Catch ex As Exception
End Try

Exceptions get caught here, but the problem is I need to start multiple functionalities in parallel of which
is only one of them. To do that, I believe launching those functionalities via Task.Run would be the best bet.

The actual functionality of the worker function in my case involves extensive DB and
activity and during this time, the UI responsiveness is driving me towards going in for

Also, have tried launching the task and Waiting for it so as to ensure evaluation of the result. However, my UI gets blocked when I do

Any help is really appreciated.

usr usr
Answer Source

Probably, what you want is

Dim task1 = RefreshCompanyCodesAsync()

That way there is no wrapper task. RefreshCompanyCodesAsync already is non-blocking which is important so that you don't freeze the UI.

You should avoid ContinueWith. Prefer await because it marshals you back to the UI thread.

I'll need to write this in C#:

var task1 = RefreshCompanyCodesAsync();
await Task.WhenAny(task1); //Wait without throwing.