VB.NET Task Factory Catching Errors

Associate
Joined
14 Mar 2007
Posts
1,667
Location
Winchester
Hi all hopefully somebody knows vb.net to help me out with this:

What this code does do is starts a new task on the thread pool to go and extract users, once it has done that bit it should update the UI (via a viewmodel) with the status.

It does work however if there are no errors, however being a connection to a server, it could error at any time and I also can raise a custom error on the userdata.extractusers sub if the current user is not found.

The error is correctly thrown in userdata.extract users but it does not propagate up through the tree back to the error block.

The issue is with the child task.continuewith..... if I remove that the error propagates as expected.

So does anybody know how to capture the error regardless of the child task?

cheers

Code:
Dim userDataTask As Task = Task.Factory.StartNew(New Action(Sub() UserData.ExtractUsers())).ContinueWith(Sub(t) pSplashViewModel.Message = "Loading Resources")

            Try
                userDataTask.Wait()
            Catch ae As AggregateException

                Throw ae.Flatten

            End Try
 
Associate
OP
Joined
14 Mar 2007
Posts
1,667
Location
Winchester
Thanks the issue was I was thinking about it far too much, an error does propagate back when you use continue with but only after it has ran the final part of the call on the thread, which in reality made little difference but from a clarity point of view there was no to run the second call if there is a db error.

I was also not using wait correctly, from what I understand using wait basically means that the calling thread simply waits for the results of all the tasks to complete from the thread pool before proceeding, so in effect it was running synchronously anyway and was never updated the UI correctly after the call from the DB, which is what I needed to do.: from what I understand all threads need to complete before the UI thread takes over again to actually update the screen. WPF can be a pain to do the most simple things.

Anyway to catch the error you simply need an inline function as such so something like this:

Code:
Private Sub getlogdata()
            LogData.Extract()

            Dim getResources As task = Task.Factory.StartNew(New Action(Sub() finalise()), CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default)
            getResources.ContinueWith(Sub(t) If t.IsFaulted Then ThreadPool_ErrorHandler(t) Else pSplashViewModel.Message = "Finalising before load.......")

        End Sub

        Private Sub ThreadPool_ErrorHandler(ByRef parTask As task)


            'copy error generated via task
            Dim copyException As AggregateException = parTask.Exception
            'ensure taks is disposed of after this method ends
            parTask.Dispose()
            'create a new task on the owner thread to raise the error to the client and do other tasks.
            Dim errorView As task = Task.Factory.StartNew(New Action(Sub() OpenErrorView(copyException)), CancellationToken.None, TaskCreationOptions.None, pContext)

        End Sub
 
Back
Top Bottom