Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

multithreading - C# allow simultaneous console input and output

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.

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

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

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.

EDIT: 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);

            }
        }

    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...