Process.Exited不会被调用,即使EnableRaisingEvents设置为true [英] Process.Exited is never called, even though EnableRaisingEvents is set to true

查看:4281
本文介绍了Process.Exited不会被调用,即使EnableRaisingEvents设置为true的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可执行文件,当我手动运行它运行正常,并且它的存在,因为它应该与预期输出。但是,当我与方法波纹管运行它时,Process.Exited事件从来没有发射。我想起了Process.EnableRaisingEvents通知

I have an executable, which runs fine when i run it manually, and it exists as it should with the expected output. But when i run it with the method bellow, the Process.Exited event is never fired. Notice that i have remembered the Process.EnableRaisingEvents

protected override Result Execute(RunExecutable task)
{
    var process = new Process();
    process.StartInfo.Arguments = task.Arguments;
    process.StartInfo.FileName = task.ExecutablePath;
    process.StartInfo.CreateNoWindow = true;
    process.StartInfo.UseShellExecute = false;
    process.StartInfo.RedirectStandardOutput = true;
    process.EnableRaisingEvents = true;

    process.Exited += (sender, args) =>
    {
        processSync.OnNext(new Result
        {
            Success = process.ExitCode == 0,
            Message = process.StandardOutput.ReadToEnd()
        });
        processSync.OnCompleted();
    };
    process.Start();

    return processSync.First();;
}

的问题是一样的,如果我使用Process.WaitForExit(),而不是反应扩展,等待退出事件。

The problem is the same, if i use Process.WaitForExit() instead of reactive extensions, to wait for the exit event.

另外,如果我有另一种说法,它产生另一个输出运行过程中,存在的罚款。

Also, if i run the process with another argument, which produces another output, it exists fine.

这似乎有事情做与 process.StartInfo.RedirectStandardOutput = TRUE; ,因为当我禁用此功能,它的工作原理。但是这可能只是另一个问题的症状

It seems to have something to do with the process.StartInfo.RedirectStandardOutput = true; since when i disable this, it works. But that could just be a symptom of another problem.

任何帮助表示赞赏: - )

Any help is appreciated :-)

推荐答案

有在你的代码中的僵局。 标准输出只是一种命名管道,其中有一个小缓冲区来从一个进程传输数据到另一个。如果缓冲区已满,写作过程中必须等待阅读过程中检索缓存一些数据。

There's a deadlock in your code. 'Standard output' is just a kind of named pipe, which has a small buffer to transfer data from one process to the other. If the buffer is full, the writing process has to wait for the reading process to retrieve some data from the buffer.

所以,你必须启动的进程可能会等你来从标准输出读取,但你都在等着你开始阅读之前进程完成 - >僵局

So the process you have started might wait for you to read from the standard output, but you are waiting for the process to finish before you start reading -> deadlock.

解决方案是在进程运行连续读 - 只需拨打 StandardOutput.ReadToEnd()你打电话之前 WaitForExit()
如果你想不阻塞当前线程读取,你可以使用 BeginOutputReadLine() OutputDataReceived 事件。

The solution is to read continuously while the process is running - just call StandardOutput.ReadToEnd() before you call WaitForExit(). If you want to read without blocking the current thread, you can use BeginOutputReadLine() and the OutputDataReceived events.

这篇关于Process.Exited不会被调用,即使EnableRaisingEvents设置为true的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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