使用C#,我想自动化一个第三方Windows命令行程序。通常情况下,它是一个交互式控制台,您发送命令,它可能会提示详细信息,发送回结果并显示提示以请求更多命令。通常情况如下:
我使用了.NET类Process和ProcessStartInfo,并利用标准输入/输出/错误流的重定向功能。
由于该程序在等待命令时返回“Prompt>”(重要提示:末尾没有“\n”),因此我无法使用myProcess.BeginOutputReadLine()。但是,我必须使用线程,因为我必须同时侦听stdout和stderr。这就是为什么我使用了线程和线程安全队列来实现生产者/消费者模式的原因。"您可以使用异步读取操作来避免这些依赖关系及其死锁潜力。或者,您可以通过创建两个线程并在单独的线程上读取每个流的输出来避免死锁条件。"源自:http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput%28v=vs.100%29.aspx。有了这个设计,所有如下的序列都能正常工作: * cmd -> result with no err(stdout上有内容,stderr上没有) * cmd -> error(stderr上有内容,stdout上没有) 没有问题。然而,对于一个特定的命令——在执行过程中提示输入密码——不起作用: * 主线程主循环永远循环
c:\>console_access.exe
Prompt> version
2.03g.2321
Prompt>
我使用了.NET类Process和ProcessStartInfo,并利用标准输入/输出/错误流的重定向功能。
public ConsoleAccess()
{
if (!File.Exists(consoleAccessPath)) throw new FileNotFoundException(consoleAccessPath + " not found");
myProcess = new Process();
ProcessStartInfo myProcessStartInfo = new ProcessStartInfo(consoleAccessPath, ""); // even "2>&1" as argument does not work; my code still hangs
myProcessStartInfo.CreateNoWindow = true;
myProcessStartInfo.UseShellExecute = false;
myProcessStartInfo.RedirectStandardOutput = true;
myProcessStartInfo.RedirectStandardError = true;
myProcessStartInfo.RedirectStandardInput = true;
//myProcessStartInfo.ErrorDialog = true; // I tried, to no avail.
myProcess.StartInfo = myProcessStartInfo;
outputQueue = new ConcurrentQueue<string>(); // thread-safe queue
errorQueue = new ConcurrentQueue<string>();
myProcess.Start();
myStandardOutput = myProcess.StandardOutput;
myStandardError = myProcess.StandardError;
myStandardInput = myProcess.StandardInput;
stdOutPumper = new Thread(new ThreadStart(PumpStdOutLoop));
stdOutPumper.Start();
stdErrPumper = new Thread(new ThreadStart(PumpStdErrLoop));
stdErrPumper.Start();
string empty = getResponse(); // check for prompt
string version = getVersion(); // one simple command
}
// [...]
private void PumpStdErrLoop()
{
while (true)
{
string message = myStandardError.ReadLine();
errorQueue.Enqueue(message);
}
}
private void PumpStdOutLoop()
{
while (true)
{
bool done = false;
string buffer = "";
//int blocksize = 1024;
string prompt = "Prompt> ";
while (!done)
{
//char[] intermediaire = new char[blocksize];
//int res = myStandardOutput.Read(intermediaire, 0, blocksize);
//buffer += new string(intermediaire).Substring(0, res);
byte b = (byte)myStandardOutput.Read(); // I go byte per byte, just in case the char[] above is the source of the problem. To no avail.
buffer += (char)b;
done = buffer.EndsWith(prompt);
}
buffer = buffer.Substring(0, buffer.Length - prompt.Length);
outputQueue.Enqueue(buffer);
}
}
由于该程序在等待命令时返回“Prompt>”(重要提示:末尾没有“\n”),因此我无法使用myProcess.BeginOutputReadLine()。但是,我必须使用线程,因为我必须同时侦听stdout和stderr。这就是为什么我使用了线程和线程安全队列来实现生产者/消费者模式的原因。"您可以使用异步读取操作来避免这些依赖关系及其死锁潜力。或者,您可以通过创建两个线程并在单独的线程上读取每个流的输出来避免死锁条件。"源自:http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput%28v=vs.100%29.aspx。有了这个设计,所有如下的序列都能正常工作: * cmd -> result with no err(stdout上有内容,stderr上没有) * cmd -> error(stderr上有内容,stdout上没有) 没有问题。然而,对于一个特定的命令——在执行过程中提示输入密码——不起作用: * 主线程主循环永远循环
if (errorQueue.Count == 0 && outputQueue.Count == 0) { System.Threading.Thread.Sleep(500); }
* 等待stdout的线程永远等待
byte b = (byte)myStandardOutput.Read();
* 等待一行stderr的线程永远等待
string message = myStandardError.ReadLine();
我不明白为什么byte b = (byte)myStandardOutput.Read();
没有输出消息“password:”。什么也没发生。我从未得到过第一个“p”。我觉得我遇到了死锁情况,但是我不理解为什么。有什么问题吗?(我认为这并不是非常相关,但我在Windows 7 32位上使用MS Visual Studio 2010的.NET 4.0上尝试了上述方法。)