【发布时间】:2015-04-29 08:34:06
【问题描述】:
如何保证 MS Word 进程退出(使用 C# 互操作库)?
我目前的做法:
// 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);
来源:
c# MSOffice Interop Word will not kill winword.exe
Disposing of Microsoft.Office.Interop.Word.Application
Microsoft.Interop object won't quit or "release"
【问题讨论】:
-
再次检查您是否违反了两点规则,并且您正在处理您创建的每个办公对象,就像您的示例中的对象一样。如果您仍然看到该过程,我也不会感到惊讶,因为您在清理代码后立即检查它。
-
是的,一切都是时机——我现在意识到你得等一会儿。谢谢
标签: c# process ms-word office-interop quit