简体   繁体   English

System.Diagnostic.Process中的死锁

[英]Deadlock in System.Diagnostic.Process

I have a small console application and I want to read the output in C#. 我有一个小的控制台应用程序,我想读取C#中的输出。 Therefore I've created this code snippet. 因此我创建了此代码段。 The command prompt opens, but nothing is displayed. 命令提示符将打开,但不显示任何内容。

System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
process.StartInfo.FileName = DirectoryPath + "Test.exe";
process.StartInfo.Arguments = "-showAll";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.Start();
process.WaitForExit(2000);
String strOutput = process.StandardOutput.ReadToEnd();

If I remove UseShellExecute , RedirectStandardOutput and the last line, the command prompt opens and the Test.exe is shown, but I need the output as String and so I have to use these attributes to read the StandardOutput 如果我删除UseShellExecuteRedirectStandardOutput和最后一行,命令提示符将打开并显示Test.exe ,但我需要输出为String,因此我必须使用这些属性来读取StandardOutput

I've also tried to set a timeout of 2 seconds ( process.WaitForExit(2000) ), but the empty command prompt does not close after 2 seconds. 我也尝试将超时设置为2秒( process.WaitForExit(2000) ),但空命令提示符在2秒后没有关闭。

If I close the empty command prompt manually in debug mode, the variable strOutput has my requested information. 如果我在调试模式下手动关闭空命令提示符,则变量strOutput具有我请求的信息。

To avoid deadlock you have to read the output stream before you wait to exit. 为避免死锁,您必须在等待退出之前读取输出流。 So try : 所以尝试:

    System.Diagnostics.Process process = new System.Diagnostics.Process();
    process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
    process.StartInfo.FileName = DirectoryPath + "Test.exe";
    process.StartInfo.Arguments = "-showAll";
    process.StartInfo.UseShellExecute = false;
    process.StartInfo.RedirectStandardOutput = true;
    process.Start();
    String strOutput = process.StandardOutput.ReadToEnd();
    process.WaitForExit();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法使用System.Diagnostic.Process运行nbtstat - Unable to run nbtstat using System.Diagnostic.Process 获取密码提示C#System.Diagnostic.Process的通知 - Getting notified of password prompt C# System.Diagnostic.Process 什么是WinRT(C#)上的System.Diagnostic.Process的等价物? - What's the equivalent of the System.Diagnostic.Process on WinRT (C#)? 如何知道是否已System.Diagnostic.Process由于退出超时? - How to know if a System.Diagnostic.Process has exited due to timeout? 我可以捕获用C#的System.Diagnostic.Process()启动的命令行应用程序的APPCRASH吗? - Can I catch APPCRASH of command line app launched with C#'s System.Diagnostic.Process()? 如何抑制使用System.Diagnostic.Process执行的exe的对话框? - How do I supress dialogs from an exe that is executed using System.Diagnostic.Process? 在C#中使用System.Diagnostic.Process及其UninstallString卸载程序 - Uninstall program using System.Diagnostic.Process and its UninstallString in C# 使用System.Diagnostic.Process.Start(“FileName”)启动Kill进程 - Kill process started with System.Diagnostic.Process.Start(“FileName”) System.Diagnostic.Process.Start()是否正确运行.exe文件 - Does System.Diagnostic.Process.Start() runs a .exe file correctly 使用System.Diagnostic Process时,我会错过进程开始和捕获输出开始之间的一些输出行吗? - When using System.Diagnostic Process, will I miss some output lines between the start of process and start of capturing output?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM