ReadLine()回调 [英] ReadLine() Callback

查看:166
本文介绍了ReadLine()回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户在我的控制台中键入命令时,我需要将其发送到我创建的Java进程(使用StreamWriter)。有什么方法可以做ReadLine类型的回调,所以当用户在控制台中输入内容时,我可以读取它,然后传递给我的StreamWriter?

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代码:

private void UserCommand(string text)
{
    if(string.Equals(text, "save"))
    {
        inputWriter.WriteLine("/save-all");
    }
}


推荐答案

不直。与GUI编程不同,控制台程序不是事件驱动的。你必须调用 Console.ReadLine ,它会阻塞当前线程并等待,直到用户按Enter键。然后你可以调用 UserCommand

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 your UserCommand.

如果你想等待用户的输入做其他事情,必须使用至少两个线程,一个是工作,一个等待 ReadLine 返回(然后调用任何你想调用的函数...)

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...)

这篇关于ReadLine()回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆