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

时间:2015-03-04 09:09:36

标签: c# multithreading

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

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

     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 <= Int32.Parse(iNrOfThreads.Text); i++)
            {
                //fileName2 = fileNameSmall + i.ToString() + ".docx";
                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);

                // To copy a file to another location and 
                // overwrite the destination file if it already exists.
                //System.IO.File.Copy(sourceFile, destFile, true);

                //Thread thread = new Thread(() => 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();              
            } 

1 个答案:

答案 0 :(得分:1)

我通过声明变量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();               
        //}
    }

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