当我启动我的应用程序并打开 word 文件时,我在任务管理器中看到 3 个进程 winword.exe。
在我调用“关闭”功能后,1 进程 winword.exe 关闭。当我在析构函数中调用 worddoc.close() 或 wordapp.quit() 时,出现异常“无法使用已与其底层 RCW 分离的 COM 对象”。
public class WordHelper
{
private object nullobj = System.Reflection.Missing.Value;
public string context = "";
Microsoft.Office.Interop.Word.Document doc = new Document();
Microsoft.Office.Interop.Word.Application wordApp = new Application();
public WordHelper(string FileName)
{
//Open word file
}
//somefunction fo work with file
public void CloseWord()
{
doc.Close();
wordApp.Quit();
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(wordApp);
}
~WordHelper()
{
//i got exception
doc.Close();
wordApp.Quit();
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(wordApp);
}
}
我如何称呼我的班级
WordHelper wddoc = new WordHelper("C:\\Test Word\\Test.docx");
wddoc.CloseWord(); //this line i use and can close 1 process not 3
//One process close after i close application
最后我想关闭我的应用程序打开的所有 winword.exe,我想在析构函数中关闭它们。最后,我需要关闭我的应用程序打开的所有“winword.exe”,我需要在析构函数中关闭它们。