C# 如何以编程方式确定Word应用程序冻结

C# 如何以编程方式确定Word应用程序冻结,c#,ms-word,pdf-generation,office-interop,docx,C#,Ms Word,Pdf Generation,Office Interop,Docx,请分享你的想法!我必须检查文件夹并转换PDF中具有不同扩展名的一组文档 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using Microsoft.Office.Interop.Word; namespace ConsoleApplication7 { class Program { static voi

请分享你的想法!我必须检查文件夹并转换PDF中具有不同扩展名的一组文档

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.Office.Interop.Word;
namespace ConsoleApplication7
{
    class Program
    {
        static void Main(string[] args)
        {

            Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
            object oMissing = System.Reflection.Missing.Value;
            word.Visible = false;
            word.ScreenUpdating = false;


            object aa = WdOpenFormat.wdOpenFormatAuto;
            string errorMessage = null;


            word.DisplayAlerts = WdAlertLevel.wdAlertsNone;

            //selection extension 

            var allExtentionGroupFiles = Directory.GetFiles(@"C:\path", "*.*").
                Where(s=>!s.Contains("~$") && (s.EndsWith(".docx") 
                || s.EndsWith(".doc")
                || s.EndsWith(".docm")
                || s.EndsWith(".dotx")
                || s.EndsWith(".dotm")
                || s.EndsWith(".dot")
                || s.EndsWith(".mht")
                || s.EndsWith(".mhtml")
                || s.EndsWith(".rtf")
                || s.EndsWith(".txt")
                || s.EndsWith(".xml")
                || s.EndsWith(".odt")
                || s.EndsWith(".wps"))).
                GroupBy(s=>s.Substring(s.LastIndexOf('.'))).OrderBy(s=>s.Key);

            foreach (var currentExtentionGroup in allExtentionGroupFiles)
            {

                Console.WriteLine("-->>{0}", currentExtentionGroup.Key);
                foreach (var currentDoc in currentExtentionGroup)
                {

                    Object filename = (Object)currentDoc;



                    try
                    {
                        //open current document

                        Document document = word.Documents.Open(filename,ConfirmConversions:aa,OpenAndRepair:true,Revert:true);

                        document.Activate();


                        object outputFileName = currentDoc.Replace(currentExtentionGroup.Key, ".pdf").Insert(10, "TEST");
                        object fileFormat = WdSaveFormat.wdFormatPDF;


                        document.SaveAs(ref outputFileName,
                        ref fileFormat, ref oMissing, ref oMissing,
                        ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                        ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                        ref oMissing, ref oMissing, ref oMissing, ref oMissing);



                        document.Close();

                    }
                    catch (Exception e1)
                    {
                        errorMessage = e1.ToString();

                    }
                }
            }

word.Quit();

}

    }
}

代码正在工作,问题是当我打开一个文档或任何允许的扩展都正常工作时,但假设有人更改了文件夹c:\path已损坏文档Word上或文件夹c:\path已损坏文档Word中示例DoSomething.exe DoSomething.doc的扩展名以停止响应,并且当我尝试手动打开此文件时,会出现一个模式窗口文件转换。不幸的是,据我所知,Office对象模型没有提供任何检测Office应用程序冻结或从冻结中恢复的方法。您甚至不需要破坏文档;Word到PDF的转换有时会因其他有效文档而冻结

我找到的唯一解决方案是生成另一个进程(不仅仅是线程),它对单个文档执行转换,并让主进程在有限的时间内(比如5分钟)等待转换完成。如果超时已过,则主进程应终止转换进程,并将文档标记为不可处理


您可以编写作为转换进程启动的程序,作为.NET控制台应用程序,该应用程序通过命令行参数接收Word文档的完整路径。

我有类似的情况-一个解决方案是创建一个包含两个线程的子进程,一个线程与Word交互,另一个线程是“看门狗”。。。“看门狗”线程将反复检查是否出现某个模式窗口,以及是否存在预定义的超时。。。在任何一种情况下,它都会终止字进程,然后等待另一个线程结束-如果另一个线程没有在预定义的时间内结束,它会终止另一个线程

这工作正常,但我观察到,在同样的情况下,硬杀死word会导致一些不愉快的副作用,从临时文件没有被清理到一些word设置被破坏


我最终使用了一个第三方库进行转换,根本不需要安装Word。。我对这个解决方案非常满意,它的性能要好得多,如果有问题的文档,我会得到一个异常,我可以相应地处理它。。。我正在使用的图书馆是一个商业图书馆。。。如果这是您的选择,我可以提供一个链接…

谢谢您的回答我也考虑过这种解决方法,我只是想确保没有其他选择