gpt4 book ai didi

c# - 将控制台输入和输出重定向到文本框

转载 作者:太空宇宙 更新时间:2023-11-03 16:54:00 24 4
gpt4 key购买 nike

您好,在此先致谢

我正在(非常努力地)尝试将控制台输入和输出重定向到文本框。到目前为止,输出工作正常,但问题出在输入上。例如,我无法执行将执行以下操作的简单程序:

Console.WriteLine("请输入您的姓名:");字符串名称 = Console.ReadLine();Console.WriteLine("你好"+ name);

我无法做到这一点的原因是程序必须在等待用户输入他/她的名字并按回车键时停止。如果我在新线程上等待用户输入,那么主 GUI 线程将卡住并且文本框永远无法接收 KeyPress。这件事让我完全难住了。任何建议(或更好的代码)将不胜感激。

干杯

最佳答案

下面的代码是一个控制台应用程序,它调用另一个控制台应用程序而不是 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));
}
}

关于c# - 将控制台输入和输出重定向到文本框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2883993/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com