运行两个任务(Outlook VSTO)时出现内存不足异常

时间:2018-09-26 14:26:56

标签: c# task vsto

我正在为Outlook 2016开发c#加载项。在某些过程中,我需要删除邮件项目类别和用户属性。我试图通过同时从收件箱/已发送文件夹中删除属性来做到这一点。为此,我开始了2个新的tasks

Task.Factory.StartNew(() => RunRemovingExistingVisualIndicationFromInboxFolder(), GlobalEnvironmentMethods.removeExistingVisualIndicationFromInboxFolderCancellationToken.Token);
Task.Factory.StartNew(() => RunRemovingExistingVisualIndicationFromSentFolder(), GlobalEnvironmentMethods.removeExistingVisualIndicationFromInboxFolderCancellationToken.Token);

在下面的代码中,该函数删除了这些属性(例如,我仅在“收件箱”文件夹中附加处理邮件项目。注意:相同的代码用于“已发送”文件夹。我仅更改了Outlook文件夹以使用“已发送”文件夹。)

    public static void RunRemovingExistingVisualIndicationFromInboxFolder()
    {
        NLogMethods.GetInstance().WriteInfoLog("[ADDITIONALVISUALINDICATIONSETTINGSFORM -> RUNREMOVEEXISTINGVISUALINDICATION()]: Processing " + "\"" + "Inbox" + "\"" + " folder started.");
        Outlook.Items folderItemsInbox = null;
        folderItemsInbox = OutlookMethods.currentOAppNameSpace.Stores[JSONMethods.ReadAddInSetting().AddInPreferredMailBoxStoreName].GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox).Items;

        if (folderItemsInbox.Count > 0)
        {
            for (int i = 1; i <= folderItemsInbox.Count; i++)
            {
                totalProcessedInboxMailItems++;
                if (GlobalEnvironmentMethods.removeExistingVisualIndicationFromInboxFolderCancellationToken.IsCancellationRequested)
                {
                    GlobalEnvironmentMethods.globalProcessingRemovingVisualIndicationTaskState = false;
                    GlobalEnvironmentMethods.removeExistingVisualIndicationFromInboxFolderCancellationToken.Dispose();
                    GlobalEnvironmentMethods.removeExistingVisualIndicationFromInboxFolderCancellationToken = null;
                    GlobalEnvironmentMethods.ServiceProcedures.ReleaseComObject(new[] { folderItemsInbox });
                    totalProcessedInboxMailItems = 0;
                    NLogMethods.GetInstance().WriteWarnLog("[ADDITIONALVISUALINDICATIONSETTINGSFORM -> RUNREMOVEEXISTINGVISUALINDICATION()]: The task was cancelled by user.");

                    return;
                }
                else
                {
                    Outlook.MailItem mailItemObjectInbox = null;
                    mailItemObjectInbox = folderItemsInbox[i] as Outlook.MailItem;
                    if (mailItemObjectInbox != null)
                    {
                        if (mailItemObjectInbox.Class == Outlook.OlObjectClass.olMail)
                        {
                            bool mailItemChanged = false;
                            if (mailItemObjectInbox.Categories != null)
                            {
                                if (mailItemObjectInbox.Categories.Contains(AddInEnvironmentMethods.CRM_CATEGORY_NAME.ToLower()) || mailItemObjectInbox.Categories.Contains(AddInEnvironmentMethods.CRM_CATEGORY_NAME.ToUpper()))
                                {
                                    //When mail item has many categories, our need remove from string only our "CRM" category, other categories, include char "," will be applied correctly by Outlook automatically;
                                    //i.e. if mail item has 3 cat.: Green category, CRM, Yellow category -> after removing "CRM" cat. category text was: Green category, , Yellow category, but don't worry, Outlook will apply this string absolutely correctly;
                                    //Note: Mail item categories is case sensitive;
                                    mailItemChanged = true;
                                    if (mailItemObjectInbox.Categories.Contains(AddInEnvironmentMethods.CRM_CATEGORY_NAME.ToLower()))
                                    {
                                        mailItemObjectInbox.Categories = mailItemObjectInbox.Categories.Remove(mailItemObjectInbox.Categories.IndexOf(AddInEnvironmentMethods.CRM_CATEGORY_NAME.ToLower()), AddInEnvironmentMethods.CRM_CATEGORY_NAME.Length);
                                    }
                                    else if (mailItemObjectInbox.Categories.Contains(AddInEnvironmentMethods.CRM_CATEGORY_NAME.ToUpper()))
                                    {
                                        mailItemObjectInbox.Categories = mailItemObjectInbox.Categories.Remove(mailItemObjectInbox.Categories.IndexOf(AddInEnvironmentMethods.CRM_CATEGORY_NAME.ToUpper()), AddInEnvironmentMethods.CRM_CATEGORY_NAME.Length);
                                    }
                                }
                            }

                            if (mailItemObjectInbox.UserProperties.Find(AddInEnvironmentMethods.CRM_COLUMN_NAME) != null)
                            {
                                mailItemChanged = true;
                                mailItemObjectInbox.UserProperties.Find(AddInEnvironmentMethods.CRM_COLUMN_NAME).Delete();
                            }

                            if (mailItemChanged)
                            {

                                mailItemObjectInbox.Save();
                            }

                            GlobalEnvironmentMethods.ServiceProcedures.ReleaseComObject(new[] { mailItemObjectInbox });
                        }
                        else
                        {
                            GlobalEnvironmentMethods.ServiceProcedures.ReleaseComObject(new[] { mailItemObjectInbox });
                        }
                    }
                }
            }
        }
        else
        {
            GlobalEnvironmentMethods.ServiceProcedures.ReleaseComObject(new[] { folderItemsInbox });
        }

        GlobalEnvironmentMethods.globalProcessingRemovingVisualIndicationTaskState = false;
        GlobalEnvironmentMethods.removeExistingVisualIndicationFromInboxFolderCancellationToken.Dispose();
        GlobalEnvironmentMethods.removeExistingVisualIndicationFromInboxFolderCancellationToken = null;
        NLogMethods.GetInstance().WriteInfoLog("[ADDITIONALVISUALINDICATIONSETTINGSFORM -> RUNREMOVEEXISTINGVISUALINDICATION()]: Processing " + "\"" + "Inbox" + "\"" + " folder completed.");
    }

“收件箱”文件夹中处理邮件的数量= 988。 但是,有时此函数会捕获“内存不足”异常。 我不明白为什么会这样...

注意:此代码行发生异常:

if (mailItemObjectInbox.Categories != null) //Out of memory ex...

我在做什么不正确?谢谢

0 个答案:

没有答案