我有一个包装控制台应用程序的GUI,以改善用户体验。我正在使用Process类来分离控制台可执行文件。我希望控制台窗口出现,我将手动编写缩写输出。然后,一旦完成可执行文件,控制台窗口将关闭,控制权将传递回GUI。
但是,它是一个交互式程序,要求我重定向StandardIn和StandardOut。问题是重定向输入的行为会阻止所有输出进入控制台。
我已经包含了所有输出代码,但是已经注释掉了。实际上,将打开一个控制台窗口,并出现提示,等待用户输入。如果我取消注释RedirectStandardIn,则会出现窗口,但只是保持空白。我是否误解了Process.StandardInput的作用?
class HandleExecutable
{
...
public void callExecutable(string executable, string args, string inputStr)
{
string commandLine = executable;
ProcessStartInfo psi = new ProcessStartInfo(commandLine);
psi.UseShellExecute = false;
psi.LoadUserProfile = false;
//psi.RedirectStandardOutput = true;
//psi.RedirectStandardError = true;
/* UNCOMMENT BELOW WILL CAUSE NO OUTPUT TO BE PUT TO THE CONSOLE */
//psi.RedirectStandardInput = true;
psi.WindowStyle = ProcessWindowStyle.Minimized;
psi.Arguments = args;
Process p = new Process();
p.StartInfo = psi;
try
{
p.Start();
//p.StandardInput.WriteLine(inputStr);
//p.BeginOutputReadLine();
//p.BeginErrorReadLine();
if (outputHandler != null) p.OutputDataReceived += outputHandler;
if (errorHandler != null) p.ErrorDataReceived += errorHandler;
p.WaitForExit();
p.Close();
p.Dispose();
}
catch (Exception ex)
{
Console.Error.WriteLine(ex.Message);
}
}
}