简体   繁体   English

Outlook VSTO崩溃以及减少内存消耗的方法

[英]Outlook VSTO Crash And Ways to Reduce Memory Consumption

Hey Guys I am a newbie in office addin programming and now I am working on creating an addin for outlook. 大家好我是Office插件编程的新手,现在我正在为Outlook创建插件。 The purpose of this outlook addin is to fetch all incoming mails in each account's inbox and send them to my server. 此Outlook插件的目的是获取每个帐户的收件箱中的所有传入邮件,并将它们发送到我的服务器。 I've successfully done it with Microsoft.Office.Interop.Outlook , though I have some problems with it. 我已经使用Microsoft.Office.Interop.Outlook成功完成了此操作 ,尽管我遇到了一些问题。 Here is my code http://pastebin.com/TwnmrYeY and below are the problems 这是我的代码http://pastebin.com/TwnmrYeY ,下面是问题

  1. As far as I know the work can't be done in the main UI thread so I create another thread that runs seperately from the main thread. 据我所知,该工作无法在主UI线程中完成,因此我创建了另一个与主线程分开运行的线程。 I loop the AccessEmails function to get new incoming mails, since ItemsEvents_ItemAddEventHandler can't handle it if there are a bunch of incoming mails. 我循环访问AccessEmails函数以获取新的传入邮件,因为如果有大量传入邮件,ItemsEvents_ItemAddEventHandler无法处理它。 The problem is the background thread sometimes won't stop when the outlook is already closed. 问题是,当Outlook已关闭时,后台线程有时不会停止。
  2. I want to wait for the synchronization send/receive to finish before fetch all the emails, I've tried to use Application.Session.SyncObjects and add event handler for SyncStart,SyncEnd, and OnError but still it is often crashed. 我想等待同步发送/接收完成,然后再获取所有电子邮件,我尝试使用Application.Session.SyncObjects并为SyncStart,SyncEnd和OnError添加事件处理程序,但仍然经常崩溃。 The crash happen when the synchronization is ongoing but I close the outlook application. 当同步正在进行但我关闭Outlook应用程序时,发生崩溃。
  3. How to reduce memory consumption while iterating through all emails in an inbox, I use Outlook.Items to iterate all the emails and it consumes a pretty large memories? 如何在遍历收件箱中的所有电子邮件时减少内存消耗 ,我使用Outlook.Items遍历所有电子邮件,并且消耗了大量内存?

I hope you guys can help me since I've been stuck pretty long in these stuffs. 我希望你们能对我有所帮助,因为我被这些东西困扰了很长时间。

Firstly, Outlook Object Model cannot be used on a secondary thread in an Outlook addin - starting with Outlook 2013, Outlook will raise an exception as soon as it detects access to one of its objects on a secondary thread. 首先,无法在Outlook插件的辅助线程上使用Outlook对象模型-从Outlook 2013开始,Outlook一旦检测到对辅助线程上的对象之一的访问,便会引发异常。 Note that external exes marshal all OOM calls to the primary Outlook thread even if in that exe OOM is used on a secondary thread. 请注意,即使外部exe在第二个线程上使用OOM,外部exes也封送所有对主Outlook线程的OOM调用。 To ve able to access various Outlook obejcts on a secondary thread, you need to use either Extended MAPI (C++ or Delphi) or the a wrapper like Redemption (its RDO family of objects can be used in secondary threads ). 为了能够访问辅助线程上的各种Outlook对象,您需要使用扩展MAPI(C ++或Delphi)或像Redemption这样的包装器(它的RDO对象系列可以在辅助线程中使用 )。

Secondly, while Application.Quit event mostly won't fire, you can still detect the last Explorer or Inspector closing (Explorer.Close nad Inspector.Close events) - as soon as the last Outlook window is closed, Outlook will exit. 其次,虽然Application.Quit事件通常不会触发,但是您仍然可以检测到最后一个Explorer或Inspector关闭(Explorer.Close和Inspector.Close事件)-一旦关闭了最后一个Outlook窗口,Outlook就会退出。 That can also help prevent Outlook for exiting: if yo uhold a reference to an Inspector or Explorer object (even if it is not visible), Outlook will stay running. 这也可以帮助防止Outlook退出:如果您拥有对Inspector或Explorer对象的引用(即使它不可见),Outlook将保持运行状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM