How can I guarantee the MS Word process exits (using C# Interop library)?
My current approach:
// close word document ("doc")
((_Document)doc).Close(SaveChanges: WdSaveOptions.wdDoNotSaveChanges);
if (doc != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(doc);
doc = null;
GC.Collect();
GC.WaitForPendingFinalizers();
}
// close word application ("word")
((_Application)word).Quit(false);
if (word != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(word);
word = null;
GC.Collect();
GC.WaitForPendingFinalizers();
}
// and here is how it fails
Process[] pname = Process.GetProcessesByName("WINWORD");
Assert.IsTrue(pname.Length == 0);
Sources:
c# MSOffice Interop Word will not kill winword.exe
Disposing of Microsoft.Office.Interop.Word.Application
Microsoft.Interop object won't quit or "release"