Word应用程序未在线程或并行进程中关闭

在将docx / doc保存到pdf之后,下面的代码通常可以正常工作并且单词被打开和关闭但是当在线程中使用以下代码或者并行循环它没有时,是否有任何想法? 我已经提供了以下所有代码。

这是函数中使用的代码工作正常。

wordApp = new Microsoft.Office.Interop.Word.Application(); Microsoft.Office.Interop.Word.Document wordDocument = wordApp.Documents.Open(sourceFile, false); wordDocument.ExportAsFixedFormat(destFile, WdExportFormat.wdExportFormatPDF); object saveOption = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges; object originalFormat = Microsoft.Office.Interop.Word.WdOriginalFormat.wdOriginalDocumentFormat; object routeDocument = false; if (wordDocument != null) ((_Document)wordDocument).Close(ref saveOption, ref originalFormat, ref routeDocument); if (wordApp != null) ((_Application)wordApp).Quit(ref saveOption, ref originalFormat, ref routeDocument); wordDocument = null; wordApp = null; 

下面是我尝试调用上面代码的Parallel.For代码:

  Parallel.For(1, Int32.Parse(iNrOfThreads.Text), new ParallelOptions { MaxDegreeOfParallelism = Int32.Parse(iNrOfThreads.Text) }, i => { fileName = fileNameLarge + i.ToString() + ".doc"; fileName2 = fileNameLarge + i.ToString() + ".pdf"; string sourceFile = System.IO.Path.Combine(sourcePath, fileName); string destFile = System.IO.Path.Combine(targetPath, fileName2); GeneratePDFWithProgressWithCreate(sourceFile, destFile); }); 

以下是生成线程的for循环,我试图调用上面的代码:

  for (int i = 1; i  GeneratePDFWithProgress(sourceFile, destFile + ".pdf")); Thread thread = new Thread(() => GeneratePDFWithProgressWithCreate(sourceFile, destFile)); thread.Name = "Thread" + i.ToString(); thread.IsBackground = true; thread.SetApartmentState(ApartmentState.MTA); thread.Start(); } 

我通过声明变量local来解决它

从原始代码

  wordApp = new Microsoft.Office.Interop.Word.Application(); 

我改成了

  Microsoft.Office.Interop.Word.Application wordAppPrivate = new Microsoft.Office.Interop.Word.Application(); 

所以现在函数的本地是这个函数的完整代码,用我创建的线程调用

  void GeneratePDFWithProgressWithCreate(string wordFilename, string pdfFilename) { // Update Progress bar to see start of threads UpdateProgress(); // Setup Word Application Microsoft.Office.Interop.Word.Application wordAppPrivate = new Microsoft.Office.Interop.Word.Application(); Microsoft.Office.Interop.Word.Document wordDocument = wordAppPrivate.Documents.Open(wordFilename, false); wordDocument.ExportAsFixedFormat(pdfFilename, WdExportFormat.wdExportFormatPDF); object saveOption = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges; object originalFormat = Microsoft.Office.Interop.Word.WdOriginalFormat.wdOriginalDocumentFormat; object routeDocument = false; if (wordDocument != null) ((_Document)wordDocument).Close(ref saveOption, ref originalFormat, ref routeDocument); if (wordAppPrivate != null) ((_Application)wordAppPrivate).Quit(ref saveOption, ref originalFormat, ref routeDocument); if (wordDocument != null) System.Runtime.InteropServices.Marshal.ReleaseComObject(wordDocument); if (wordAppPrivate != null) System.Runtime.InteropServices.Marshal.ReleaseComObject(wordAppPrivate); wordDocument = null; wordAppPrivate = null; //GC.Collect(); // force final cleanup! // Update progress bar to see finishing the conversion UpdateProgress(); //} } 

我希望这能帮助有类似问题的人!