ReadLine() 回调

发布于 2024-10-20 14:02:22 字数 303 浏览 6 评论 0原文

当用户在我的控制台中键入命令时,我需要将其发送到我创建的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

放赐 2024-10-27 14:02:22

是的。

string input = Console.ReadLine();
UserCommand(input);

Yeah.

string input = Console.ReadLine();
UserCommand(input);
归属感 2024-10-27 14:02:22

不直接。与 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 your UserCommand.

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

﹉夏雨初晴づ 2024-10-27 14:02:22

您可能可以使用Console.OpenStandardInput 来获取输入流并使用该流的异步函数。

    static string command = "";
    static System.IO.Stream s;
    static bool quit = false;
    static byte[] buf = new byte[1];
    static void Main(string[] args)
    {
        s = Console.OpenStandardInput();
        s.BeginRead(buf, 0, 1, new AsyncCallback(s_Read), null);
        while (!quit)
        {
            // Do something instead of sleep
            System.Threading.Thread.Sleep(1000);
            Console.WriteLine("Sleeping");
        }
        s.Close();
    }
    public static void s_Read(IAsyncResult target)
    {
        if (target.IsCompleted)
        {
            int size = s.EndRead(target);
            string input = System.Text.Encoding.ASCII.GetString(buf);
            if (input.EndsWith("\n") || input.EndsWith("\r"))
            {
                if (command.ToLower() == "quit") quit = true;
                Console.Write("Echo: " + command);
                command = "";
            }
            else
                command += input;

            s.BeginRead(buf, 0, 1, new AsyncCallback(s_Read), null);
        }
    }

You can probably use Console.OpenStandardInput to get the input stream and use the asynchronous functions of the stream.

    static string command = "";
    static System.IO.Stream s;
    static bool quit = false;
    static byte[] buf = new byte[1];
    static void Main(string[] args)
    {
        s = Console.OpenStandardInput();
        s.BeginRead(buf, 0, 1, new AsyncCallback(s_Read), null);
        while (!quit)
        {
            // Do something instead of sleep
            System.Threading.Thread.Sleep(1000);
            Console.WriteLine("Sleeping");
        }
        s.Close();
    }
    public static void s_Read(IAsyncResult target)
    {
        if (target.IsCompleted)
        {
            int size = s.EndRead(target);
            string input = System.Text.Encoding.ASCII.GetString(buf);
            if (input.EndsWith("\n") || input.EndsWith("\r"))
            {
                if (command.ToLower() == "quit") quit = true;
                Console.Write("Echo: " + command);
                command = "";
            }
            else
                command += input;

            s.BeginRead(buf, 0, 1, new AsyncCallback(s_Read), null);
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文