Freigeben über


More on getting Outlook to shut down

I recently posted some code showing how to get Outlook to shut down.

I contended that you shouldn't have to call ReleaseCOMObject as is shown in some MSDN articles to get Outlook to shut down.

Indeed, David Mortenson who has worked on interop here at Microsoft warns further about ReleaseCOMObject.

One other point that David has made relating to the technique I showed for getting Outlook to shutdown (set variables containing Outlook OM objects to null and force a garbage collection and ) is that you actually sometimes have to write this code.

// Set objects using Outlook OM objects to null

GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();

For my simple example where the objects I was setting to null were just variables pointing to Outlook OM objects, you wouldn't have to do this, but if you are setting a class to null which has a finalizer and that class has a variable containing an Outlook OM object, you would have to do this to make sure that class releases all it's OM objects it is holding on to.

Comments