其他分享
首页 > 其他分享> > 互操作单词过程不会立即关闭

互操作单词过程不会立即关闭

作者:互联网

我正在使用多线程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);
   }

每个线程都会打开一个单词应用程序,并应该在导出文档后将其关闭.使用相同的方法,我将导出的PDF转换为PNG,然后删除临时PDF(排除了代码,因为我认为它与问题无关).

该过程可以完成工作,但是由于我要转换成千上万的文档,因此我在后台遇到了很多文字处理,从而耗尽了我的RAM.这些进程最终会自行关闭,但速度非常慢,因此我在后台遇到了越来越多的进程.这不会在所有机器上也不会在每个文档上都发生,所以我不确定出了什么问题.

有没有办法强制关闭它们?我已经尝试了通过名称杀死进程的方法,但是这要求应用程序窗口可见,这是我不想要的.

解决方法:

使用Microsoft.Office.Interop.Excel dll时遇到了类似的问题.我发现我必须释放我一直使用的所有COM对象,然后Excel才能从Windows Task Manager的“进程”选项卡中消失.明确地说,我将关闭该应用程序,但是在释放COM对象之前,它的过程在任务管理器中将保持可见.尝试这样的事情:

private void ReleaseComObjects(bool isQuitting)
{
    try
    {
        if (isQuitting)
        {
            workbook.Close(false, missing, missing); 
            excelApplication.Quit();
        }
        Marshal.ReleaseComObject(workbooks);
        Marshal.ReleaseComObject(workbook);
        if (worksheets != null) Marshal.ReleaseComObject(worksheets);
        Marshal.ReleaseComObject(worksheet);
        Marshal.ReleaseComObject(excelApplication);
        workbook = null;
        worksheets = null;
        worksheet = null;
        excelApplication = null;
    }
    catch { }
    finally { GC.Collect(); }
}

private void ReleaseComObjects()
{
    ReleaseComObjects(false);
}

标签:multithreading,wpf,c,ms-word,office-interop
来源: https://codeday.me/bug/20191121/2051116.html