Best Practices : How to quit Outlook application after automation from Visual Studio .NET client?

When you automate a Microsoft Outlook application from Microsoft Visual Basic .NET or Microsoft Visual C# .NET, the Outlook application does not quit when you call the Quit method. You can notice the application is closed, but still if we notice either the application or Outlook is running in behind the scenes. To make sure that the Office application quits, make sure that your automation code meets the following criteria:    

Use System.Runtime.InteropServices.Marshal.ReleaseComObject when you have finished using an object. This decrements the reference count of the RCW.

To release the reference to the variable, set the variable equal to Nothing or Null.

Use the Quit method of the Office application object to tell the server to shut down.

You can try this C#.Net code snippet:

 //If you are using Visual C# .NET, reference the code for the ReleaseObj() function:
  
 private void ReleaseObj(object obj)
 {
     try 
     {
         System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
     }
     catch {}
     finally 
     {
         obj = null;
     }
 }

In this scenario, you can use the GC.Collect() method and the GC.WaitForPendingFinalizers() method after you release the last object. Because the runtime performs garbage collection on the RCW, the GC.Collect() method forces the garbage collector to run and might release any references that the RCW still has. The GC.Collect() method tries to reclaim the maximum memory that is available.

Note: That this does not guarantee that all memory will be reclaimed.

For more reference and detailed information, read blog post series by Matt.

mstehle- The CDOs and CDONTS of Messaging Development - OOM.NET- Part # 1

mstehle- The CDOs and CDONTS of Messaging Development - OOM.NET- Part # 2

mstehle- The CDOs and CDONTS of Messaging Development - OOM.NET