的Process.Start与“UseShellExecute =真正的'不返回损坏的Word文档文档、Start、Process、UseShellExecute

2023-09-06 23:47:25 作者:冷月

我想打印的PDF,PPT,并从我的窗口服务使用的ShellExecute word文档。

I am trying to print pdf, ppt, and word documents from my windows service using ShellExecute.

Process printingProcess = new Process
                        {
                            StartInfo =
                                {
                                    FileName = filePath,
                                    WindowStyle = ProcessWindowStyle.Hidden,
                                    CreateNoWindow = true,
                                    UseShellExecute = true,
                                    Verb = "Print"
                                }
                        };
printingProcess.Start();

这适用于大多数情况下。但是,对于Word文档是腐败的的Process.Start方法永远不会完成,服务挂起。

This works in most cases. But for word documents that are corrupt, the Process.Start method never completes and the service hangs.

基本上,字弹出的坏文件!修复对话框。我希望该服务,以确定这个词是不是在玩好和杀死进程,并继续在其队列中的下一个文档。

Basically, word pops up the "bad document! repair" dialog. I want the service to identify that word is not playing nice and kill the process and proceed with the next document in its queue.

我应该怎么办?

[更新]

大家好,这里是code重现该问题:

Guys, here is the code to reproduce the issue:

static void Main(string[] args)
{
    string filePath = @"d:\corruptdocument.docx";

    PrintDocument(filePath);

    Console.WriteLine("Completed!");
    Console.ReadKey();
}

private static void PrintDocument(string filePath)
{
    Process printingProcess = new Process
                                {
                                    StartInfo =
                                        {
                                            FileName = filePath,
                                            WindowStyle = ProcessWindowStyle.Hidden,
                                            CreateNoWindow = true,
                                            UseShellExecute = true,
                                            Verb = "Print"
                                        }
                                };
    using (printingProcess)
    {
        Console.WriteLine("Starting process...");
        printingProcess.Start();
        Console.WriteLine("Completed start...");
    }
}

下面是截图: http://twitpic.com/23jwor

推荐答案

好Guyz,这里是我发现了!

Okay Guyz, here is what I found out!

ShellExecute的字使用DDE。所以的Process.Start()方法返回后,才命令完成(在我的情况,打印文档)。 [不知道这是正确的,但ATLEAST这是我与文字的经验]

ShellExecute for word uses DDE. So the process.Start() method returns only after the command is complete (in my case, "printing the document"). [Not sure if this is accurate, but atleast that is my experience with word]

那么,什么是我们的选择?

So what are our options?

作为@Hans提到的,使用COM互操作。 运行打印作业在一个单独的线程和等待线程完成了pdefined间隔$ P $这个时间间隔后终止相应的文字处理。

我选择了选项2,因为我正在处理其他文件类型如PDF,PPT,我才懒得改实施! :)

I chose option 2 as I was dealing with other document types like PDF,PPT and I was too lazy to change the implementation! :)