Marshal.ReleaseComObject(wordApp)需要15-20秒

时间:2016-04-05 12:26:54

标签: c# .net ms-word office-interop com-interop

我有疑问,这行代码是否正常:

Marshal.ReleaseComObject(wordApp)

需要大约20秒?我尝试使用FinalReleaseComObject()方法,时间仍然太长。有人能告诉我如何解决它?

WordApp.Documents documents = null;
WordApp.Document doc = null;
WordApp.Application wordApp = new WordApp.Application();
wordApp.Visible = false;
wordApp.ScreenUpdating = false;
try
{
   documents = wordApp.Documents;
   doc = documents.Open();
   doc.ExportAsFixedFormat();
   doc.Close(ref saveChanges);
}
finally
{
   wordApp.Quit();
   if (doc != null) Marshal.ReleaseComObject(doc);             // takes few ms
   if (documents != null) Marshal.ReleaseComObject(documents); // takes few ms
   if (wordApp != null) Marshal.ReleaseComObject(wordApp);     // takes 15-20 seconds
   wordApp = null;
}

0 个答案:

没有答案