ReadLine() 回调
当用户在我的控制台中键入命令时,我需要将其发送到我创建的 Java 进程(使用 StreamWriter)。有没有办法执行 ReadLine 类型的回调,以便当用户在控制台中键入内容时,我可以读取它,然后将其传递给我的 StreamWriter?
伪代码:
private void UserCommand(string text)
{
if(string.Equals(text, "save"))
{
inputWriter.WriteLine("/save-all");
}
}
When a user types a command into my console, I need to send it to a Java process (Using StreamWriter) that I created. Is there any way to do a ReadLine sort of callback, so when a user types something in the console, I can read it, then pass it to my StreamWriter?
Pseudo Code:
private void UserCommand(string text)
{
if(string.Equals(text, "save"))
{
inputWriter.WriteLine("/save-all");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的。
Yeah.
不直接。与 GUI 编程不同,控制台程序不是事件驱动的。您必须显式调用
Console.ReadLine
,这会依次阻塞当前线程并等待用户按下 Enter 键。然后您可以调用您的UserCommand
。如果您想在等待用户输入时执行其他操作,则必须使用至少两个线程,一个正在工作,另一个等待 ReadLine 返回(然后调用您想要的任何函数)打电话...)
Not directly. Unlike in GUI programming, console programs are not event-driven. You'll have to call
Console.ReadLine
explicitely which in turns blocks the current thread and waits until the user presses the Enter key. Then you can call yourUserCommand
.If you want to do other things while waiting for the user's input you'll have to use at least two threads, one which is working and one waiting for
ReadLine
to return (and then calling whatever function you want to call...)您可能可以使用Console.OpenStandardInput 来获取输入流并使用该流的异步函数。
You can probably use
Console.OpenStandardInput
to get the input stream and use the asynchronous functions of the stream.