Release COM Object in C#

后端 未结 2 970
萌比男神i
萌比男神i 2021-01-05 20:25

I know this has been discussed earlier but I couldn\'t find a satisfactory answer.

I have an e-mail file (.msg) which I open like below and then call Display.

2条回答
  •  生来不讨喜
    2021-01-05 21:03

    Doing things like this:

    mail = (Microsoft.Office.Interop.Outlook.MailItem)oApp.Session.OpenSharedItem(fileName);
    

    will cause the references to not be disposed of even when you call ReleaseComObject because the reference to the child object hasn't been disposed of properly.

    You should make the calls like this:

    session = oApp.Session;
    mail = (Microsoft.Office.Interop.Outlook.MailItem)session.OpenSharedItem(fileName);
    

    And you should dispose of each of these sub-objects, like session, in turn.

提交回复
热议问题