一个进程如何阻止另一个进程(它产生的)?

How can a process be blocking another process (that it spawned)?

我编写了一个用于启动和停止特定进程的实用程序。现在,在对其进行测试时,它似乎以某种方式阻止了它生成的进程!

它使用命名系统事件(参见 System.Threading.EventWaitHandle)。启动进程后,等待事件设置:

private static int StartRavenDB(string fileName, string workingDirectory, string arguments)
{
    var process = new Process
    {
        StartInfo =
        {
            FileName = fileName,
            WorkingDirectory = workingDirectory,
            Arguments = arguments,
            UseShellExecute = false,
            CreateNoWindow = true,
            RedirectStandardInput = true,
            RedirectStandardOutput = true
        }
    };

    process.Start();

    var eventWaitHandle = new EventWaitHandle(false, EventResetMode.ManualReset, "StartStopRavenDBUtility");
    eventWaitHandle.Reset();

    eventWaitHandle.WaitOne();

    process.StandardInput.WriteLine("q");
    process.WaitForExit();

    return process.ExitCode;
}

现在,启动的 RavenDB 进程是一个侦听 localhost:8080 的 Web 服务器。

在使用上述实用程序启动该进程后不久,该进程不响应 Web 请求。它一直超时。一旦我杀死实用程序进程,一切就开始正常工作了。

郑重声明,我 100% 确定 EventWaitHandle 尚未设置 - RavenDB 进程在那里,但它的行为不正常。

我不知道发生了什么或为什么,这是一个完全独立的过程。是什么导致了这个问题?

您应该订阅 OutputDataReceived 事件或至少读取重定向的标准输出,以避免阻塞线程。来自 documentation:

These dependencies can cause deadlock conditions. When the caller reads from the redirected stream of a child process, it is dependent on the child. The caller waits for the read operation until the child writes to the stream or closes the stream. When the child process writes enough data to fill its redirected stream, it is dependent on the parent. The child process waits for the next write operation until the parent reads from the full stream or closes the stream. The deadlock condition results when the caller and child process wait for each other to complete an operation, and neither can continue. You can avoid deadlocks by evaluating dependencies between the caller and child process.