【发布时间】:2014-08-21 13:54:42
【问题描述】:
我正在使用多线程 WPF 应用程序中的互操作词库将 word 文档导出为 PDF,然后使用 MagickNet 库将它们转换为 PNG。
private void generateImagesFromOfficeDoc(Document currentDocument, CustomThread thread = null)
{
var token = tokenSource.Token;
Microsoft.Office.Interop.Word.Application wordApplication = null;
string[] filePath = currentDocument.OriginalPath.Split('\\');
currentDocument.Filename = filePath[filePath.Length - 1].Trim();
string fileNameWithoutExtension = System.IO.Path.GetFileNameWithoutExtension(currentDocument.Filename).Trim();
string destinationPath;
destinationPath = pngPath + "\\" + currentDocument.CreatedDate.Year.ToString() + "\\" + currentDocument.Specialty + "\\" + currentDocument.CreatedDate.Month + "\\" +
currentDocument.CreatedDate.Day + "\\" + fileNameWithoutExtension;
string pdfPath = magickNETTempDir + "\\" + fileNameWithoutExtension + ".pdf";
string pdfName = fileNameWithoutExtension + ".pdf";
string tempFile = magickNETTempDir + "\\" + currentDocument.Filename;
try
{
// Copying the file from the original location to the temp folder. ** This is because impersonation have issues with the interop library.
File.Copy(currentDocument.OriginalPath, tempFile);
// Create the directory if it does not exist.
if (!Directory.Exists(destinationPath))
{
Directory.CreateDirectory(destinationPath);
}
/************ CONVERTING THE DOCUMENT TO PDF ***************/
wordApplication = new Microsoft.Office.Interop.Word.Application();
// Opening the word document
var wordDocument = wordApplication.Documents.Open(tempFile);
var pageCount = wordDocument.ComputeStatistics(Microsoft.Office.Interop.Word.WdStatistic.wdStatisticPages, false);
// Exporting the document to the PDF
wordDocument.ExportAsFixedFormat(pdfPath, Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF);
// Closing the document and the application.
((Microsoft.Office.Interop.Word._Document)wordDocument).Close(false);
((Microsoft.Office.Interop.Word._Application)wordApplication).Quit(false);
}
每个线程都会打开一个 word 应用程序,并且应该在导出文档后关闭它。以同样的方法,我将导出的 PDF 转换为 PNG,然后删除临时 PDF(代码被排除,因为我认为它与问题无关)。
这个过程完成了它的工作,但是由于我正在转换数千个文档,所以我在后台得到了相当多的文字处理,占用了我的 RAM。这些进程最终会自行关闭,但速度非常慢,因此我最终会在后台处理越来越多的进程。这不会发生在所有机器上,也不会发生在每个文档上,所以我不确定出了什么问题。
有没有办法强制关闭它们?我已经尝试过按名称杀死进程的方法,但这需要应用程序窗口可见,这是我不想要的。
【问题讨论】:
-
您是否尝试过在单独的应用程序域中运行此代码并在完成后卸载该域?
-
您是否尝试过创建一次
Word.Application并将其用于所有Document.Open呼叫?我过去曾将 Word 用于其拼写检查库,我发现它会在调用Close/Quit后保持打开一段时间。 -
为什么不能使用一个Interop 实例?如果您尝试对此进行多线程处理,问题是可能只有 1 个 IO 线程。
标签: c# wpf multithreading ms-word office-interop