7

Any ideas why the following code does not exit the Outlook 2007 process created via COM interop?

Microsoft.Office.Interop.Outlook.Application app = new Microsoft.Office.Interop.Outlook.Application();

var item = app.Session.OpenSharedItem("C:\\test.msg") as Microsoft.Office.Interop.Outlook.MailItem;
string body = item.HTMLBody;
int att = item.Attachments.Count;

(item as Microsoft.Office.Interop.Outlook._MailItem).Close(Microsoft.Office.Interop.Outlook.OlInspectorClose.olDiscard);
System.Runtime.InteropServices.Marshal.ReleaseComObject(item);

(app as Microsoft.Office.Interop.Outlook._Application).Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
System.Diagnostics.Debugger.Break();

An almost identical snippet using Word works, so I wonder if I'm forgetting to clean up something...

Nikolaos
  • 1,449
  • 2
  • 15
  • 19

4 Answers4

11

You have a 3rd COM object referenced in your code: app.Session. This must also be released correctly. Try this code:

Microsoft.Office.Interop.Outlook.Application app = null;
Microsoft.Office.Interop.Outlook.NameSpace session = null;
Microsoft.Office.Interop.Outlook.MailItem item = null;

try {
    app = new Microsoft.Office.Interop.Outlook.Application();
    session = app.Session;
    item = session.OpenSharedItem("C:\\test.msg") as Microsoft.Office.Interop.Outlook.MailItem;

    string body = item.HTMLBody;
    int att = item.Attachments.Count;

    (item as Microsoft.Office.Interop.Outlook._MailItem).Close(Microsoft.Office.Interop.Outlook.OlInspectorClose.olDiscard);

    (app as Microsoft.Office.Interop.Outlook._Application).Quit();
} finally {
    if(item != null) {
        System.Runtime.InteropServices.Marshal.FinalReleaseComObject(item);
    }
    if(session != null) {
        System.Runtime.InteropServices.Marshal.FinalReleaseComObject(session);
    }
    if(app != null) {
        System.Runtime.InteropServices.Marshal.FinalReleaseComObject(app);
    }
}
Christian Hayter
  • 30,581
  • 6
  • 72
  • 99
  • Just to drive the point home, setting variables to null is absolutely necessary. This is the case even if the object isn't `System.__ComObject` (directly or derived). Once I factored this in, Outlook hung around for about 5 seconds more then closed. – Tyler Montney Sep 12 '22 at 18:52
2

I don't know the specifics of the Office COM Interops, but here's some code suggested from an MSDN article. It suggests the double collect/wait and clearing of the pointers helps with the RCW wrapper cleanup.

item = null;
app.Quit();
app = null;
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();

That url however also suggests

while (Marshal.ReleaseComObject(app) > 0) { }

which I would personally strongly advise against if you can help it, as you've basically just destroyed that RCW for your AppDomain (as the article points out).

[Edit: Also the .Net garbage collector behaves very differently when inside a debugger vs release code, so testing this outside of the debugger is very important]

dwhiteho
  • 636
  • 5
  • 4
0

Try following after app.Quit();

// ReleaseComObject(xApp);
GC.WaitForPendingFinalizers();
GC.Collect();
Mo Patel
  • 2,321
  • 4
  • 22
  • 37
TonyP
  • 5,655
  • 13
  • 60
  • 94
0

Try this instead, it works for me, there will be a few second delay before it goes:

app.Quit(); //
System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
GC.Collect();
GC.WaitForPendingFinalizers();
curtisk
  • 19,950
  • 4
  • 55
  • 71