Process.WaitForExit () on the console and Windows Forms

I have a console application and an application for forms of winnings, which must be called to a remote server for some data, they make a call to the Putty command line part, plink.exe, to run the remote command via SSH.

I created a tiny class library for sharing by doing the following:

public static string RunCommand(string command, string arguments) { ProcessStartInfo startInfo = 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; } 

In the console application, everything works fine, within the framework of the winning forms, this is not an error, it seems that WaitForExit () just does not wait. I get an empty string for output. I confirmed from the remote server that the user is logged in, so it seems that the command is running.

Any ideas?

+2
source share
1 answer

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

RedirectStandardInput=true works because it forces the system to create a Writer for STDIN, which you can use to send input to the child process. In your case, the child does not need an input, he just needs the presence of input. YMMV.

+5
source

Source: https://habr.com/ru/post/1392190/


All Articles