开发者

Process.WaitForExit() on Console vs Windows Forms

I have a console app and a win forms app that both need to call out to a remote server for some data, they make a call to the command line part of Putty, plink.exe, to run a remote command over SSH.

I created a tiny class library for both to share, running the following:

public static string RunCommand(string command, string arguments) {
  ProcessStartInfo st开发者_开发知识库artInfo = new ProcessStartInfo {
      FileName = command,
      Arguments = arguments,
      UseShellExecute = false,
      CreateNoWindow = true,
      RedirectStandardOutput = true
  };
  string output = null;
  using (Process p = new Process()) {
      p.StartInfo = processStartInfo;
      p.Start();
      output = p.StandardOutput.ReadToEnd();
      p.WaitForExit();
  }
  return output;
}

Under the console application everything works fine, under the win forms it doesn't error, it seems that WaitForExit() just doesn't wait. I get an empty string for output. I've confirmed from the remote server the user logged in, so it seems the command has run.

Any ideas?


Under Windows Console applications have STDIN, STDOUT, and STDERR. Windowed applications do not. When you create a process under a Console application the STDIN etc. are inherited by the child application. This does not happen in the Windowed application.

The RedirectStandardInput=true works because it makes the system create a Writer for the STDIN that you can use to send input to the child process. In your case the child doesn't need the input it just needs the presence of the input. YMMV.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜