您好,并提前感谢
我正在努力(非常努力)将控制台输入和输出重定向到文本框。到目前为止输出工作正常,但麻烦在于输入。 例如,我无法执行将执行以下操作的简单程序:
Console.WriteLine(“请输入你的名字:”); string name = Console.ReadLine(); Console.WriteLine(“你好”+名字);
我无法实现这一点的原因是因为程序必须在等待用户输入他/她的名字时按下并输入。如果我等待新线程上的用户输入,则主GUI线程冻结,文本框永远不会收到KeyPress。这件事让我完全难过。任何建议(或更好的代码)将不胜感激。
干杯
答案 0 :(得分:0)
下面的代码是一个控制台应用程序,它调用另一个控制台应用程序来完成一些工作而不是WinForm应用程序,但您可以轻松地替换事件处理程序(TransformProcessOutputDataReceived和TransformProcessErrorDataReceived)以输出到文本框而不是TextWriter。不幸的是,它没有直接解决等待用户输入的被叫控制台应用程序的问题。下面的代码确实通过标准输入将一些输入输入到被叫控制台应用程序,所以也许你可以用同样的方式从你的windows应用程序提供它。
希望这很有帮助,我有一段时间让自己最初工作,对不起,我忘记了原来的参考资料,这是我刚才使用过的。
private static void ProcessRetrievedFiles(List<string> retrievedFiles)
{
Console.WriteLine();
Console.WriteLine("Processing retrieved files:");
Console.WriteLine("---------------------------");
Console.WriteLine();
foreach (string filePath in retrievedFiles)
{
if (String.IsNullOrEmpty(filePath)) continue;
Console.WriteLine(filePath);
Process transformProcess = new Process();
string baseOutputFilePath = Path.Combine(ExportDirectory, Path.GetFileNameWithoutExtension(filePath));
transformProcess.StartInfo.FileName = TransformerExecutablePath;
transformProcess.StartInfo.Arguments = string.Format(
"-i:\"{0}\" -x:\"{1}\" -o:\"{2}.final.xml\"",
filePath,
string.Empty,
baseOutputFilePath);
transformProcess.StartInfo.UseShellExecute = false;
transformProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
transformProcess.StartInfo.RedirectStandardError = true;
transformProcess.StartInfo.RedirectStandardOutput = true;
transformProcess.StartInfo.RedirectStandardInput = true;
transformProcess.EnableRaisingEvents = true;
//attach the error/output recievers for logging purposes
transformProcess.ErrorDataReceived += TransformProcessErrorDataReceived;
transformProcess.OutputDataReceived += TransformProcessOutputDataReceived;
ProcessBridgeFileOutputWriter = new StreamWriter(
baseOutputFilePath + ".log",
false);
ProcessBridgeFileOutputWriter.AutoFlush = true;
transformProcess.Start();
transformProcess.BeginOutputReadLine();
transformProcess.BeginErrorReadLine();
//the exe asks the user to press a key when they are done...
transformProcess.StandardInput.Write(Environment.NewLine);
transformProcess.StandardInput.Flush();
//because we are not doing this asynchronously due to output writer
//complexities we don't want to deal with at this point, we need to
//wait for the process to complete
transformProcess.WaitForExit();
ProcessBridgeFileOutputWriter.Close();
ProcessBridgeFileOutputWriter.Dispose();
//detach the error/output recievers
transformProcess.ErrorDataReceived -= TransformProcessErrorDataReceived;
transformProcess.OutputDataReceived -= TransformProcessOutputDataReceived;
}
}
static void TransformProcessOutputDataReceived(object sender, DataReceivedEventArgs e)
{
if (!string.IsNullOrEmpty(e.Data))
{
ProcessBridgeFileOutputWriter.WriteLine(e.Data);
}
}
static void TransformProcessErrorDataReceived(object sender, DataReceivedEventArgs e)
{
if (!string.IsNullOrEmpty(e.Data))
{
ProcessBridgeFileOutputWriter.WriteLine(string.Format("ERROR: {0}", e.Data));
}
}