简体   繁体   中英

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. The purpose of this outlook addin is to fetch all incoming mails in each account's inbox and send them to my server. I've successfully done it with Microsoft.Office.Interop.Outlook , though I have some problems with it. Here is my code http://pastebin.com/TwnmrYeY and below are the problems

  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. 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. The problem is the background thread sometimes won't stop when the outlook is already closed.
  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. The crash happen when the synchronization is ongoing but I close the outlook application.
  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?

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. 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. 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 ).

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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