Interacting word processes do not close immediately
I am exporting text documents to PDFs using the interop word library in a multi-threaded WPF application and then converting them to PNG using the MagickNet library.
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);
}
Each thread opens a word-attachment and must close it after exporting the document. In the same method, I convert the exported PDF to PNG and then delete the temporary PDF (the code is excluded as I think it is not related to the problem).
The process gets the job done, but since I am converting thousands of documents, I end up with quite a few text processes in the background, feeding on their RAM. The processes eventually get closed, but they are very slow, and thus I get more and more processes in the background. This does not happen on all machines, nor on every document, so I'm not sure what is going wrong.
Is there a way to force them to close? I tried the method of killing processes by their name, but it requires the application window to be visible, which I don't want.
source to share
I am having similar problems when using a dll Microsoft.Office.Interop.Excel
. I found out that I had to release all the COM objects I was using before Excel
disappearing from the Windows Task Manager tab. To be clear, I will close the application, but its process will remain visible in the Task Manager until I release the COM objects. Try something like this:
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);
}
source to share