2012-04-25 53 views
1

我不认为关于此主题的任何以前的问题都为这个问题提供了答案。我使用psexec来执行远程exe文件。当我在命令行中运行它时,我得到了exe文件的输出。 psexec.exe \\machine C:\somename.exe
当我使用C锐利的进程执行它挂起或它不重定向输出。对于一些EXE的超时和一些重定向的标准输出是空的,错误包含Exe退出代码0.有什么办法来捕获输出?PsExec v 1.98输出重定向问题

ProcessStartInfo startInfo = new ProcessStartInfo(); 
     startInfo.FileName =GetPsExecPath(); 
     startInfo.Arguments = arguments; 
     Debug.WriteLine(arguments); 
     startInfo.UseShellExecute = false; 
     startInfo.RedirectStandardError = true; 
     startInfo.RedirectStandardOutput = true; 
     startInfo.CreateNoWindow = true; 
     Process process = new Process(); 
     process.StartInfo = startInfo; 
     process.Start(); 
     process.WaitForExit(); 
     error = process.StandardError.ReadToEnd(); 
     output = process.StandardOutput.ReadToEnd(); 
     Debug.WriteLine(error); 
     Debug.WriteLine(output); 
     process.close(); 

编辑:SOLN 所以问题主要是引起PSEXEC抛出了很多其他的事情到标准错误,因此在我们阅读它们(的顺序,whtever可能导致死锁如果我们使用ReadToEnd的) 。所以如果我们使用BeginOutputReadLine,它就像一个魅力!

+1

向我们展示您用于执行psexec的代码。 – 2012-04-25 05:28:37

+0

也如果我不重定向输出和错误它执行并退出与代码0.只有当我重定向它导致这些pblms。 – sriram 2012-04-25 05:44:09

+0

当你通过命令行运行它时,它会返回数据?使用您在程序中传递的相同参数? – 2012-04-25 05:51:34

回答

4

此代码片段导致死锁的几率非常高。因为您首先阅读StandardError,然后阅读StandardOutput。这意味着process.StandardOutput.ReadToEnd()不会被调用,直到进程退出。这意味着,当psexec填充足够的字符时,将无法刷新它的stdout输出缓冲区。这意味着它会阻止并因此永不终止。僵局城市。

如果交换两个调用,你会有更好的赔率,大多数程序会将大量输出发送到stdout。但是,如果由于某种原因psexec将很多字符写入标准错误,那么仍然存在非零的死锁概率。您完全可以通过使用BeginOutputReadLine和BeginErrorReadLine来消除这种情况。

+1

谢谢.. thts真棒。它解决了这个问题。 – sriram 2012-04-25 14:05:34