递归循环不与BackgroundWorker一起从Outlook PST文件获取信息

时间:2017-09-29 13:57:50

标签: vb.net outlook-addin

我试图遍历Outlook PST文件,从每个文件夹中的每封电子邮件中收集一些属性信息。使用递归循环的子文件夹。我试图通过BackgroundWorker实现这一目标,以便我的表单UI不会冻结。但问题是它没有按预期工作,它没有递归循环并在Inbox中进行了几百次迭代后退出,因为我有20多个文件夹和子文件夹以及40,000多封电子邮件。

Dim IndexDict = New Dictionary(Of String, String)
Const PropName As String = "http://schemas.microsoft.com/mapi/proptag/0x300B0102"
Dim skey As String
Dim oPA As Outlook.PropertyAccessor

Sub PrepareIndexing
    Dim dFolderR As Outlook.Folder
    dFolderR = oNspace.Folders.Item(dName)
    BackgroundWorker1.RunWorkerAsync(dFolderR)
End Sub

Sub BackgroundWorker1_DoWork(sender As Object, e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    Dim f As Outlook.Folder = CType(e.Argument, Outlook.Folder)
    Dim worker As BackgroundWorker = CType(sender, BackgroundWorker)
    InitIndexing(f, worker, e)
End Sub

Sub InitIndexing(f As Outlook.Folder, ByVal worker As BackgroundWorker, ByVal e As DoWorkEventArgs)
    If f.Folders.Count = 0 Then Exit Sub
    For c = 1 To f.Folders.Count
        Dim Folder As Outlook.Folder = f.Folders.Item(c)
        For Each ml In Folder.Items
            oPA = ml.PropertyAccessor
            skey = oPA.BinaryToString(oPA.GetProperty(PropName))
            If Not DestinationIndexDict.ContainsKey(skey) Then
                IndexDict.Add(skey, Folder.FolderPath)
            End If
        Next
        InitIndexing(Folder, worker, e)
    Next
End Sub

1 个答案:

答案 0 :(得分:0)

任何OOM调用都应该在主线程上完成。

Outlook对象模型在STA COM服务器中运行。这意味着所有OOM调用都在主线程上执行。如果你正在构建一个从另一个线程(主线程除外)进行OOM调用的加载项,那么你的调用需要被正确编组回到线程0.对于非托管代码,你应该使用CoMarshalInterface或CoMarshalInterThreadInterfaceInStream来编组你的跨线程的对象指针。这将确保您的OOM调用在线程0上正确执行。如果您正在编写托管代码,通过PIA使用OOM通过PIA实际上将为您处理此编组。请在Outlook Crashes When Using Outlook Object Model in Multiple Threads文章中详细了解相关内容。