Home Looped Tasks Submitted Inside Try/Catch Blocks
Reply: 0

Looped Tasks Submitted Inside Try/Catch Blocks

user1377
1#
user1377 Published in April 26, 2018, 1:45 pm

I have a background task "submitter" that handles one or more scheduled tasks:

...
While sdr.read()
    ...
    Dim oBackground As New Background
    Task.Run(Sub() CallByName(oBackground, sProcessKey, CallType.Method, iPQID))
End While

Before you ask, yes, the tasks are thread-safe. :)

I want the submitter to end (go back to sleep) while the tasks operate; awaiting the next heartbeat, but I need to catch and handle exceptions in each thread.

The only way I've come up with to do this is to create a new task for each background job to be run and handle the exception there, but this seems inefficient:

Create new Task for each background job that then, submits the actual job and waits to see if an error happened and handles it:

Private Sub NewJob(sProcessKey, iPQID)
    Dim t As Task
    ....
    While sdr.read()
        ...
        Dim oBackground As New Background
        t = Task.Run(Sub() CallByName(oBackground, sProcessKey, CallType.Method, iPQID))
        Try
            t.Wait()
        Catch ex As AggregateException 
            For Each IEx As Exception In ex.InnerExceptions
                HandleBackgroundException(IEx, sProcessKey)
            Next
        End Try
    End While
End Sub

Isn't there a better way to do this??

Thanks!

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.333578 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO