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.