Microsoft.Office.Interop.Word.Application处置 [英] Disposing of Microsoft.Office.Interop.Word.Application
问题描述
(有点从职位的遵循(这仍然没有答案):如何关闭/在Micorsoft.Office.Interop处理应用.Word在asp.net )
(Somewhat of a follow on from the post (which remains unanswered): How to Close/ dispose Application in Micorsoft.Office.Interop.Word in asp.net)
使用以下code
Application app = new Application();
_Document doc = app.Documents.Open("myDocPath.docx", false, false, false);
doc.PrintOut(false);
doc.Close();
我试图以编程方式打开和打印文件。
I am attempting to open and print a file programmatically.
问题是每次我运行上面的code一个新的Winword.exe进程时启动,显然这很快吞噬了所有的记忆。
The problem is each time I run the above code a new WINWORD.exe process is started and obviously this quickly eats up all the memory.
应用程序类似乎并没有包含物/关闭或类似的方法。
The application class doesn't seem to contain a dispose/close or similar method.
在一些研究I(实现),并改变了code以下内容。
After a bit of research I (realized) and changed the code to the following.
Application app = new Application();
_Document doc = app.Documents.Open(fullFilePath + ".doc", false, false, false);
doc.PrintOut(false);
doc.Close();
int res = System.Runtime.InteropServices.Marshal.ReleaseComObject(doc);
int res1 = System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
和我可以看到剩余的引用计数为零,但仍然过程?
And I can see the remaining reference count is zero but the processes remain?
PS:我使用的Microsoft.Office.Interop库的版本14
PS: I'm using Version 14 of the Microsoft.Office.Interop library.
推荐答案
也许尝试设置 DOC = NULL
并调用 GC.Collect的()
编辑,不是我自己的code我忘了,我懂了,但是这是我用处置Excel和它的工作也许你可以收集来自这样的:
Edit, not really my own code I forget where I got it but this is what I use to dispose of Excel, and it does the job maybe you can glean something from this:
public void DisposeExcelInstance()
{
app.DisplayAlerts = false;
workBook.Close(null, null, null);
app.Workbooks.Close();
app.Quit();
if (workSheet != null)
System.Runtime.InteropServices.Marshal.ReleaseComObject(workSheet);
if (workBook != null)
System.Runtime.InteropServices.Marshal.ReleaseComObject(workBook);
if (app != null)
System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
workSheet = null;
workBook = null;
app = null;
GC.Collect(); // force final cleanup!
}
这篇关于Microsoft.Office.Interop.Word.Application处置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!