Process.Start 的替代本机 api
好的,这不是“Alternative to Process.Start()”的重复,因为我的问题是这里不同。
我需要运行一个进程并等待进程执行并获取控制台的输出。
有一种方法可以将 RedirectStandardOutput 和 RedirectStandardError 设置为 true,但是这在某些机器上无法正常工作(未安装 .NET SDK),仅安装了 .NET 运行时,现在它在某些机器上工作,在某些机器上不起作用所以我们不知道问题出在哪里。
我有以下代码,
ProcessStartInfo info = new ProcessStartInfo("myapp.exe", cmd);
info.CreateNoWindow = true;
info.UseShellExecute = false;
info.RedirectStandardError = true;
info.RedirectStandardOutput = true;
Process p = Process.Start(info);
p.WaitForExit();
Trace.WriteLine(p.StandardOutput.ReadToEnd());
Trace.WriteLine(p.StandardError.ReadToEnd());
在某些机器上,这将永远挂在 p.WaitForExit() 上,而在某些机器上它可以正常工作,行为是如此随机,没有任何线索。
现在,如果我能使用 pinvoke 找到一个真正好的解决方法,我会非常高兴。
myapp.exe 只是在屏幕上写 10 条 hello world 语句。
Ok this is not duplicate of "Alternative to Process.Start()" because my question is something different here.
I need to run a process and wait till execution of process and get the output of console.
There is way to set RedirectStandardOutput and RedirectStandardError to true, however this does not function well on some machines, (where .NET SDK is not installed), only .NET runtime is installed, now it works on some machines and doesnt work on some machines so we dont know where is the problem.
I have following code,
ProcessStartInfo info = new ProcessStartInfo("myapp.exe", cmd);
info.CreateNoWindow = true;
info.UseShellExecute = false;
info.RedirectStandardError = true;
info.RedirectStandardOutput = true;
Process p = Process.Start(info);
p.WaitForExit();
Trace.WriteLine(p.StandardOutput.ReadToEnd());
Trace.WriteLine(p.StandardError.ReadToEnd());
On some machines, this will hang forever on p.WaitForExit(), and one some machine it works correctly, the behaviour is so random and there is no clue.
Now if I can get a real good workaround for this using pinvoke, I will be very happy.
myapp.exe is nothing but writing 10 hello world statements on screen.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
难道你的子进程真的永远挂起,例如。等待输入或显示不可见的错误对话框?
本机 API 是 CreateProcess,它是对应的pInvoke。
Could it be that your child process really hangs for ever, eg. waiting on input or displaying an error dialog that is not visible?
The native API is CreateProcess, and it's corresponding pInvoke.
通过调用本机代码来使用单独的解决方法并不能解决问题。 Process API 只是本机 流程函数的薄包装 - 直接使用它们只会使您的代码更加混乱并导致其他问题。
在这种情况下,听起来问题出在您的“myapp.exe”上。由于某种原因,该应用程序没有在这些机器上终止。如果您发现导致该问题的原因,您将可能能够使用 Process.Start 使该工作正常进行。
Using a separate work around by calling native code is not going to correct the problem. The Process API is just a thin wrapper around the native Process functions - using them directly is just going to make your code more confusing and cause other problems.
It sounds like the problem, in this case, is your "myapp.exe". For some reason, that application is not terminating on those machines. If you discover what is causing that, you will likely be able to make this work correctly using Process.Start.
好吧,我从某个地方得到了这个答案......
Ok I got this answer from somewhere...