1

これは 2 つのスレッドを持つプログラムです。1 つは出力用、もう 1 つは入力用です。(_ はコンソール カーソル)

Please enter a number:
12_

12 を入力している間、現在の行をクリアして上書きする出力が生成されるため、次のようになります。

Please enter a number:
Output
_

まだ入力している12を次の行に移動して、再入力する必要がないようにするにはどうすればよいですか?

前もって感謝します。

コード:

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

namespace ConsoleApplication6
{
    class Program
    {
        static void Main(string[] args)
        {
            Program prog = new Program();
            Thread t1 = new Thread(prog.getInput);
            t1.Start();
            prog.otherThread();
        }

        public void otherThread()
        {
            while (true)
            {
                System.Threading.Thread.Sleep(3000);
                ClearCurrentConsoleLine();
                Console.WriteLine("Output");
            }
        }

        public void getInput()
        {
            while (true)
            {
                string msg;
                msg = Console.ReadLine();
            }
        }

        public static void ClearCurrentConsoleLine()
        {
            int currentLineCursor = Console.CursorTop;
            Console.SetCursorPosition(0, Console.CursorTop);
            for (int i = 0; i < Console.WindowWidth; i++)
            {
                Console.Write(" ");
            }
            Console.SetCursorPosition(0, currentLineCursor);
        }
    }

ご覧のとおり、「Hello」と入力して入力しないと、3 秒後に「Output」で上書きされます。「こんにちは」と入力を上書きする前に2行目に移動したい。

4

2 に答える 2

1

カーソルの位置と変更について説明しているこの記事( Web アーカイブ)を見つけました。私はそれをかなり簡単に見つけました。

その目玉は次のとおりです。

      int left = Console.CursorLeft;
      int top = Console.CursorTop;
      Console.SetCursorPosition(15, 20);
于 2013-07-25T18:42:41.800 に答える