2

I am parsing email from Outlook from vb.net. The main form displays the total count of mail for the selected Outlook folder and allows me to override this number so that I can process 1, 10, or all. When I click a button I open a new form passing in ref to my outlook mailClient class to provide access to the mail items. The form new and show events set up things and within a try/catch make a call to an async function like so.

    Try

    AllEMails = Await ScanMailItems(ref_clsEmailClient.ECFMailToScan,progressIndicator, tokenSource.Token)

    Catch ag As AggregateException

        If ag.InnerExceptions IsNot Nothing Then

            MsgBox(ag.InnerExceptions.Count.ToString)

            For Each ex In ag.InnerExceptions
                MsgBox(ex.Message)
            Next

        End If

    Catch ex As Exception

        MsgBox(ex.Message & vbNewLine & vbNewLine & _
               ex.StackTrace)

    End Try

In this ScanMailItems() I am setting up a For Each Next to loop the mail items like so.

Dim EmailsFraction As Integer = Await Task(Of Integer).Run(
    Function()
        For i = ECFMailItems.Count To 1 Step -1
            'other code

             Throw New Exception("Test Error 1")
             UpdateFormDelegate()

             counter += 1
        Next
        Return counter
    End Function)

Return EmailsFraction

The code this far is working and in the loop I am updating controls with some delegate subs to show subject and the email body of each email that will be parsed in the loop. My trouble is that I have placed some Throw New Exception("Test Error") in the for loop and in the delegate subs to be sure they will propagate back to my try/catch in my shown event. They do not and I have tried AggregateException. I can post actual code but think it is all in how the exception propagates through the async task.

I am just learning to do this, is there a better way to set it up? The main thing is that on each iteration of the For Loop my progress bar and the other controls need to update and I will be calling into other classes.

RLester
  • 31
  • 5
  • 1
    Try as I might, I don't see your `Try/Catch` block anywhere? – Icepickle Jan 31 '18 at 22:42
  • @Icepickle, I added my try/catch and my call to delegate and included the test exception statement. This brakes the program with....(An exception of type 'System.Exception' occurred in MyProgram.exe but was not handled in user code Additional information: Test Error 1) – RLester Feb 01 '18 at 00:05
  • There's not the appropriate code here for a repro, you need to post an [MCVE](https://stackoverflow.com/help/mcve). But in general are you actually trying to use an `Async Sub`, that's only applicable to event handlers and has different exception handling semantics than an `Async Function`. Please post something that can repro the exact issue you're having. – JSteward Feb 01 '18 at 13:57
  • @JSteward I get what U say now https://msdn.microsoft.com/en-us/magazine/jj991977.aspx https://stackoverflow.com/questions/32067034/how-to-handle-task-run-exception I was thinking my async lambda Func returned int to an await-ed Task task.run(action) so the lambda return is being seen as void witch is a no no. the active SynchronizationContext is on the task of task.Run(). So my error is left on my task object and not propagating up as I need. web link say “async lambdas should only be used if they’re converted to a delegate type that returns Task (for example, Func).” – RLester Feb 02 '18 at 01:29

0 Answers0