即使Process.HasExited为true,Process.WaitForExit也不会返回 [英] Process.WaitForExit doesn't return even though Process.HasExited is true

查看:247
本文介绍了即使Process.HasExited为true,Process.WaitForExit也不会返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Process.Start启动批处理文件.批处理文件使用"START"命令并行启动多个程序,然后退出.

I use Process.Start to start a batch file. The batch file uses the "START" command to start several programs in parallel and then exits.

完成批处理文件后,Process.HasExited变为true,并且Process.ExitCode包含正确的退出代码.

Once the batch file is done Process.HasExited becomes true and Process.ExitCode contains the correct exit code.

但是当我调用Process.WaitForExit()时,它会挂起/永不返回.

But when I call Process.WaitForExit() it hangs / never returns.

以下代码演示了该问题.它创建一个批处理文件,启动它,然后打印:

The following piece of code demonstrates the problem. It creates a batch file, starts it and then prints:

Process is still running...
Batch file is done!
Process has exited. Exit code: 123
Calling WaitForExit()...

然后应打印:

WaitForExit returned.

...但是它从来没有(即使HasExited为true并且我们已经有一个ExitCode).

... but it never does (even though HasExited is true and we already have an ExitCode).

open System.IO
open System.Diagnostics
open System.Threading

let foobat = """
  START ping -t localhost
  START ping -t google.com
  ECHO Batch file is done!
  EXIT /B 123
"""

File.WriteAllText("foo.bat", foobat)

use p = new Process(StartInfo = ProcessStartInfo("foo.bat",
                                                 UseShellExecute = false,
                                                 RedirectStandardOutput = true,
                                                 RedirectStandardError = true))

let onOutput = DataReceivedEventHandler(fun _ args -> printfn "%s" args.Data)

p.OutputDataReceived.AddHandler onOutput
p.ErrorDataReceived.AddHandler onOutput

p.Start() |> ignore

p.BeginErrorReadLine()
p.BeginOutputReadLine()

while not p.HasExited do
  printfn "Process is still running..."
  Thread.Sleep(1000)

printfn "Process has exited. Exit code: %d" p.ExitCode

printfn "Calling WaitForExit()..."
p.WaitForExit()|> ignore
printfn "WaitForExit returned."

我注意到只有在批处理文件包含"START"命令并且重定向标准输出和/或标准错误时,才会发生这种情况.

I noticed that this only happens when the batch file contains "START" commands and when standard output and/or standard error are redirected.

为什么WaitForExit()永不返回?

Why does WaitForExit() never return?

等待这样的过程退出的正确方法是什么?

What's the right way to wait for such a process to exit?

仅轮询Process.HasExited是否安全,否则可能导致其他问题?

Is it safe to just poll Process.HasExited or can that result in other problems?

PS .:我只是注意到,在进程退出时,以巨大超时(肯定不会到期)的方式调用WaitForExit( 100000 ).怪异的没有超时,它将挂起.

PS.: I just noticed that calling WaitForExit(100000) with a huge timeout (that definitely doesn't expire) returns immediately when the process exits. Wierd. Without timeout it hangs.

推荐答案

在StandardOutput和StandardError的基于事件的异步处理的特定实现中,这似乎是一种人工制品(我会说"bug").

This seems to be an artifact (I'd say "bug") in the specific implementation of the event-based asynchronous handling of StandardOutput and StandardError.

我注意到,尽管我能够轻松地重现您的问题,但是只需运行您提供的代码(顺便说一下,就是优秀的代码示例!:)),该过程实际上并没有无限期地挂起.相反,一旦两个已启动的子进程本身退出,它就会从WaitForExit()返回.

I noticed that while I was able to easily reproduce your problem, simply by running the code you provided (excellent code example, by the way! :) ), the process did not actually hang indefinitely. Rather, it returned from WaitForExit() once both of the child processes that had been started had themselves exited.

这似乎是Process类实现的有意部分.特别是,在 Process.WaitForExit() 方法,完成对进程句柄本身的等待后,将检查是否已创建用于stdout或stderr的读取器;如果是这样,并且WaitForExit()调用的超时值是无限"(即-1),则该代码实际上等待读取器上的流结束.

This seems to be an intentional part of the implementation of the Process class. In particular, in the Process.WaitForExit() method, once it has finished waiting on the process handle itself, it checks to see if a reader for either stdout or stderr has been created; if so, and if the timeout value for the WaitForExit() call is "infinite" (i.e. -1), the code actually waits for the end-of-stream on the reader(s).

仅当调用BeginOutputReadLine()BeginErrorReadLine()方法时,才会创建每个阅读器.直到子进程关闭后,stdout和stderr流本身才关闭.因此,等待这些流的结尾将阻塞,直到发生这种情况为止.

Each respective reader is created only when the BeginOutputReadLine() or BeginErrorReadLine() method is called. The stdout and stderr streams are themselves not closed until the child processes have closed. So waiting on the end of those streams will block until that happens.

WaitForExit()的行为应有所不同,具体取决于是否调用了启动基于事件的流读取的任何一种方法,尤其是考虑到直接读取这些流不会 not 导致WaitForExit()以这种方式运行,从而在API中造成不一致,从而使其更加难以理解和使用.虽然我个人将其称为错误,但我想Process类的实现者有可能意识到这种不一致并故意创建了这种不一致.

That WaitForExit() should behave differently depending on whether one has called either of the methods that start the event-based reading of the streams or not, and especially given that reading those streams directly does not cause WaitForExit() to behave that way, creates an inconsistency in the API that makes it much more difficult to understand and use. While I'd personally call this a bug, I suppose it's possible that the implementor(s) of the Process class are aware of this inconsistency and created it on purpose.

无论如何,解决方法是直接读取StandardOutput和StandardError,而不使用API​​的基于事件的部分. (当然,如果一个人的代码要在那些流上等待,那么在子进程关闭之前,人们会看到相同的阻塞行为.)

In any case, the work-around would be to read StandardOutput and StandardError directly instead of using the event-based part of the API. (Though of course, if one's code were to wait on those streams, one would see the same blocking behavior until the child processes close.)

例如(C#,因为我对F#的了解不足,无法迅速将这样的代码示例拍打在一起:)):

For example (C#, because I don't know F# well enough to slap a code example like this together quickly :) ):

using System;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;

namespace TestSO26713374WaitForExit
{
    class Program
    {
        static void Main(string[] args)
        {
            string foobat =
@"START ping -t localhost
START ping -t google.com
ECHO Batch file is done!
EXIT /B 123
";

            File.WriteAllText("foo.bat", foobat);

            Process p = new Process { StartInfo =
                new ProcessStartInfo("foo.bat")
                {
                    UseShellExecute = false,
                    RedirectStandardOutput = true,
                    RedirectStandardError = true
                } };

            p.Start();

            var _ = ConsumeReader(p.StandardOutput);
            _ = ConsumeReader(p.StandardError);

            Console.WriteLine("Calling WaitForExit()...");
            p.WaitForExit();
            Console.WriteLine("Process has exited. Exit code: {0}", p.ExitCode);
            Console.WriteLine("WaitForExit returned.");
        }

        async static Task ConsumeReader(TextReader reader)
        {
            string text;

            while ((text = await reader.ReadLineAsync()) != null)
            {
                Console.WriteLine(text);
            }
        }
    }
}

希望上述解决方法或类似方法可以解决您遇到的基本问题.感谢评论者Niels Vorgaard Christensen 将我引导到WaitForExit()方法中有问题的行,以便我可以改善此答案.

Hopefully the above work-around or something similar will address the basic issue you've run into. My thanks to commenter Niels Vorgaard Christensen for directing me to the problematic lines in the WaitForExit() method, so that I could improve this answer.

这篇关于即使Process.HasExited为true,Process.WaitForExit也不会返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆