- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我很难解读关于 Process.StandardOutpout 的 MSDN 文档至于 Read(Char[], Int32, Int32) 方法是否阻塞。我的理解是它不应该阻塞,但当我将 RedirectStandardInput 设置为 true 时它似乎会阻塞。
有没有人有这方面的经验;或对我遇到的问题的一些解释?
这里的上下文是我不想等待整行(即带有行终止符),或者在读取标准输出之前等待进程退出。我也不想使用回调。 我想在进程写入时同步读取 StdOut。
这是我的代码的简化版本:
string command = @"C:\flex_sdks\flex_sdk_4.5.1.21328\bin\fcsh.exe";
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = false; # <-- if I set this to true, then
# the program hangs on
# p.StandardOutput.Read later on
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.FileName = command;
p.Start();
StringBuilder sb_stdout = new StringBuilder(1024);
char[] buffer = new char[64];
int nb_bytes_read;
while (true) {
do {
nb_bytes_read = p.StandardOutput.Read(buffer, 0, buffer.Length);
sb_stdout.Append(new string(buffer, 0, nb_bytes_read));
} while (nb_bytes_read > 0);
if (sb_stdout.ToString().EndsWith("\n(fcsh) "))
break;
Thread.Sleep(20);
}
基于我的(可能是错误的)假设 Process.StandardOutput 在使用时被破坏:
我决定尝试直接使用 Windows 的 API。我用这样的代码添加了一个答案;它工作正常(至少现在是这样)。
我创建了一个 blog entry使用我现在使用的代码。
最佳答案
实际上,就在上周,我一直在与此抗争……出于某种原因,除了 Read() 调用(ReadToEnd() 不是我需要的)之外,其他任何东西似乎都被阻止并且永远不会返回。这是我为最终让它“工作”所做的工作:
片段 1:
private bool ThreadExited = true;
private bool ExitThread = false;
private void ReadThread()
{
while (!ExitThread)
{
string Output = "";
int CharacterInt = myProcess.StandardOutput.Read();
while (CharacterInt > 0)
{
char Character = (char)CharacterInt;
Output += Character;
var MyDelegate = new delegateUpdateText(UpdateText);
Invoke(MyDelegate, Output);
Output = "";
CharacterInt = myProcess.StandardOutput.Read();
}
System.Threading.Thread.Yield();
}
ThreadExited = true;
}
片段 2:
private void InitializeProcess()
{
ThreadExited = true;
ExitThread = true;
while (!ThreadExited)
System.Threading.Thread.Sleep(1000);
ThreadExited = false;
ExitThread = false;
myProcess = new Process();
ProcessStartInfo PSI = myProcess.StartInfo;
PSI.FileName = @"cmd.exe";
PSI.UseShellExecute = false;
PSI.RedirectStandardError = false;
PSI.RedirectStandardInput = true;
PSI.RedirectStandardOutput = true;
PSI.CreateNoWindow = false;
PSI.ErrorDialog = true;
myProcess.StartInfo = PSI;
myProcess.Exited += new EventHandler(myProcess_Exited);
myProcess.EnableRaisingEvents = false;
myProcess.Start();
ReadThreadThread = new System.Threading.Thread(ReadThread);
ReadThreadThread.Start();
}
private System.Threading.Thread ReadThreadThread;
这最终对我有用。在我的例子中,我正在将文本写入文本框,但这应该很容易修改为其他内容。但是我所做的其他任何事情都由于障碍而引起了问题;出于某种原因,即使我使用反射来获取可用的字节数,调用 ReadBlock() 函数也会阻塞。从来没有让我满意。
关于c# - 为什么当 StartInfo.RedirectStandardInput 设置为 true 时 StandardOutput.Read() 会阻塞?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6655613/
我有一个服务(通常作为 NetworkService 运行)使用 StartInfo 检索进程信息。它工作正常。 但是客户希望以受限用户身份运行该服务。 (合理的要求) 根据 MS docs Star
我想打开提示命令并执行参数,所以我正在使用这段代码: System.Diagnostics.Process process = new System.Diagnostics.Process();
我有三个路径,我希望它们是传递给可执行文件的 3 个独立参数。我做的是 startinfo.arguments,但我如何将它们分开以便可执行文件知道它们是三个不同的参数? 最佳答案 您可以简单地将它们
看起来不是。 如果我将文件名转换为其短值,则 Process.Start() 有效。 Process runScripts = new Process(); runScripts.StartInfo.
我一直在测试(过去 4 天)在 iis7 (asp.net) 下启动进程的各种选项 我确实找到了解决方案。 只要我们不需要与桌面交互,只需要运行cmd(或类似的东西)解决方案很简单: w3wp 用户-
我开始一个流程 Process app = new Process(); app.StartInfo.UseShellExecute = false; app.StartInfo.FileName =
以下问题发生在 .NET Framework v3.5 上。不知道它是否适用于 v4*。 为了捕获某些进程的标准输出,我已成功使用 p.StartInfo.UseShellExecute = fals
我想在运行时切换一个进程的可见性,我有一个 Windows Form 应用程序,它通过一个进程启动,另一个控制台应用程序默认隐藏,但我想让管理员用户切换它通过复选框状态并在他们选择时显示控制台应用程序
简而言之,我正在为仿真器 MAME 编写一个前端,作为对 WPF 和 C# 的研究。 GUI 已设置,它正在正确读取 cfg,除了实际启动 MAME 之外一切正常。 在命令行 (Windows 7)
我试图通过单击表单上的按钮打开应用程序 (.exe): System.Diagnostics.Process.Start("C:\\Games\\GameDir\\Game.exe"); 应用程序执行
在我的例子中,SaveFileDialog 不会写入任何文件,但我想用来指定命令行应用程序的路径,该应用程序将在与“已保存”的位置相同的位置创建日志文件sf 对话框。 SaveFileDialog s
可以使用 UTF-8 字符串作为 StartInfo 的参数吗? 我正在尝试将 UTF-8(在本例中为日语字符串)作为控制台参数传递给应用程序。 类似这样的东西(这只是一个例子!(cmd.exe 将是
我试图上传文件并将它们转换为另一种格式,然后再将它们保存到我的网络服务器上,但出现以下错误:System.ComponentModel.Win32Exception (0x80004005):访问在
我正在尝试使用 System.Diagnostics.Process 命名空间在 C# 程序中调用 chrome.exe。 我的 chrome.exe 位于路径 C:\Program Files (x
我有一个 GUI 应用程序(在新进程中)执行“控制台”应用程序并解析输出。要重定向输出,我将 pConsole.StartInfo.RedirectStandardOutput 设置为 true。我还
我很难解读关于 Process.StandardOutpout 的 MSDN 文档至于 Read(Char[], Int32, Int32) 方法是否阻塞。我的理解是它不应该阻塞,但当我将 Redir
在查询包含我已安装的知识库文章的 wmic qfe 列表时,我尝试启动隐藏的命令提示符。 我从命令提示符中启动命令“wmic qfe list full”,这让我得到了列表。 当我从我的 C# 应用程
我在 ASP.NET C# 中有以下代码,当我在 Debug模式下测试时,它在本地系统 + 生产机器上运行良好。但是当我上传到 IIS 时它不起作用。 ProcessStartInfo startIn
我是一名优秀的程序员,十分优秀!