重定向输出时 ResGen.exe 卡住 [英] ResGen.exe stucks when redirect output

查看:35
本文介绍了重定向输出时 ResGen.exe 卡住的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试从 ResGen.exe 重定向标准输出.我使用以下代码

I try to redirect standard output from ResGen.exe. I use following code

ProcessStartInfo psi = new ProcessStartInfo( "resxGen.exe" );
psi.CreateNoWindow = true;
psi.Arguments = sb.ToString();
psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;
Process p = Process.Start( psi );
p.WaitForExit();
StreamReader sr = p.StandardOutput;
string message = p.StandardOutput.ReadToEnd();

它停留在 p.WaitForExit 上.当我关闭输出流重定向并且不读取 StandardOutput 时,它可以正常工作.

It stuck on p.WaitForExit. When I turn off output stream redirection and do not read StandardOutput it works correctly.

我做错了什么?

推荐答案

您需要在读取流后等待进程结束,否则您的代码会出现死锁.问题是你的父进程正在阻塞等待子进程完成,而子进程正在等待父进程读取输出,因此你有一个死锁.

You would need to wait for the process to end after reading the stream, otherwise you have a deadlock in your code. The problem is that your parent process is blocking waiting for the child process to finish, and the child process is waiting for the parent process to read the output, hence you have a deadlock.

这里 很好,很详细问题描述.

Here is a good and detailed description of the problem.

像这样更改代码应该可以避免死锁:

Changing your code like this should avoid the deadlock:

StreamReader sr = p.StandardOutput;
string message = p.StandardOutput.ReadToEnd();
p.WaitForExit();

这篇关于重定向输出时 ResGen.exe 卡住的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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