C#允许同时控制台输入和输出 [英] C# allow simultaneous console input and output

查看:148
本文介绍了C#允许同时控制台输入和输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过做一个简单的小游戏学习我的方式围绕C#线程。我遇到了一个问题,我可以肯定使用帮助。

I'm trying to learn my way around C# threading by making a simple little game. I've run into a bit of an issue that I could certainly use help with.

我的愿望是有同时输入和输出。线程的输出将出现在屏幕的顶部,而用户的输入可以被键入到屏幕的底部。我遇到的问题是,为了刷新屏幕,当我使用 Console.Clear(),它也擦除了用户输入的任何东西!下面我附加了一个极其简化的版本,我想做的(为了避免不必要的代码阻碍了实际问题的方式)。

My desire is to have simultaneous input and output. The output from the threads would appear at the top of the screen, while the user's input could be typed to the bottom of the screen. The problem I am running into is that in order to refresh the screen, when I use Console.Clear(), it also wipes out whatever the user is typing! Below I have attached an extremely simplified version of what I am trying to do (in order to avoid unnecessary code getting in the way of the actual issue).

请注意:虽然在此示例中,我只更新屏幕顶部的单个字符,但我的实际程序将在顶部和中间的屏幕,将不断变化与每个刻度(我计划在1.5秒使用)。

Please note: While in this example I am only updating a single character at the top of the screen, my actual program would have LOTS of text all over the top and middle of the screen that would be constantly changing with every tick (which I plan to use at 1.5 seconds).

任何想法都会很棒!我仍然是新的C#编程,并会很高兴任何帮助,你可以给:)我唯一附带的东西是设计。系统输出在顶部,用户输入在最底部由>前面。如果我这样做的方式是错误的,我没有问题折腾它的窗口和做一个不同的方式。

Any ideas would be great! I'm still new to C# programming and would be thrilled for any help you could give :) The only thing here that I am attached to is the design. System output at the top, user input at the very bottom preceeded by ">". If the way I am doing it is wrong, I have no issue with tossing it all out the window and doing it a different way.

编辑:我的目标是让屏幕顶部的输出文本每1.5秒更新一次(每次运行计数线程),同时允许用户在屏幕底部输入。然而,我使用的方法(清除然后将新内容写入屏幕)也消除了用户的输入!它使得每1.5秒任何用户输入的输入只是消失,正当地,因为这是正是Console.Clear的作用。所以我正在寻找一种新的方法来完成这项任务。

My goal here is to have the output text at the top of the screen update every 1.5 seconds (every run of the count thread) while allowing the user to type at the bottom of the screen. However, the method I am using to do that (clearing and then writing new content to the screen) is also wiping out the user's input! It makes it so that every 1.5 seconds whatever input the user is typing just disappears, and rightfully so since that's exactly what Console.Clear does. So I'm looking for a new method to accomplish this task.

简而言之:如何更新屏幕顶部/中间的文本,同时允许用户继续在屏幕底部输入?

In short: How do I update text at the top/middle of the screen while allowing the user to continue typing at the bottom of the screen?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace ConsoleApplication2
{
    class Program
    {

        static int i = 0;

        static void Main(string[] args)
        {
            Thread tickThread = new Thread(new ThreadStart(CountThread));
            Thread userThread = new Thread(new ThreadStart(UserInput));
            tickThread.Start();
            Thread.Sleep(1);
            userThread.Start();
            Thread.Sleep(20000);
            tickThread.Abort();
            userThread.Abort();
        }

        static void UserInput()
        {
            string input = "";
            while (true)
            {
                input = Console.ReadLine();
            }
        }


        static void CountThread()
        {
            while (true)
            {
                Console.Clear();
                Console.SetCursorPosition(0, 0);
                Console.WriteLine(i);
                i++;
                Console.SetCursorPosition(0, Console.WindowHeight - 1);
                Console.Write("> ");
                Thread.Sleep(1500);

            }
        }

    }
}


推荐答案

您可以设置光标位置,除了那些为用户输入设计的单元格,并写入空格符号。这将基本上清除控制台窗口的一部分。
此外,您可以使用相同的方法将输出部分渲染到控制台。

You can set cursor position in a loop for each cell in the console except those, which are designed for user's input, and write a space symbol. This will, essentially, clear a part of a console window. Also, you can use the same method to partially render output to the console.

这篇关于C#允许同时控制台输入和输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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