Keep Console.ReadLine always on last line

后端 未结 3 1642
别跟我提以往
别跟我提以往 2021-01-23 03:44

I have an application written in C# which takes continuously commands from the user via a while(true) loop and Console.ReadLine.

I also do have various background operat

3条回答
  •  野性不改
    2021-01-23 04:38

    Here's the answer to my problem:

    class Program
    {
        static void Main(string[] args)
        {
            new Messager(1000 * 2.5f);
            new Messager(1000 * 3);
            while (true)
            {
                Console.ReadLine();
            }
        }
    }
    
    public class Messager
    {
        Timer Timer;
    
        public Messager(double interval)
        {
            this.Timer = new Timer(interval);
            this.Timer.Elapsed += (sender, e) =>
            {
                int currentTopCursor = Console.CursorTop;
                int currentLeftCursor = Console.CursorLeft;
    
                Console.MoveBufferArea(0, currentTopCursor, Console.WindowWidth, 1, 0, currentTopCursor + 1);
    
                Console.CursorTop = currentTopCursor;
    
                Console.CursorLeft = 0;
    
                Console.WriteLine("message from " + this.Timer.Interval);
    
                Console.CursorTop = currentTopCursor +1;
                Console.CursorLeft = currentLeftCursor;
    
            };
            this.Timer.Start();
        }
    }
    

提交回复
热议问题