Blink text in C#

前端 未结 7 1356
隐瞒了意图╮
隐瞒了意图╮ 2021-01-14 03:17

How do i blink the text in console using C#?

7条回答
  •  我在风中等你
    2021-01-14 03:45

    I improved Matthew's code a bit using \r and some fancy use of String:

    static void Main()
    {
        string txt = "Hello, world!";
        WriteBlinkingText(txt, 500);
    }
    
    private static void WriteBlinkingText(string text, int delay)
    {
        bool visible = true;
        while (true)
        {
            Console.Write("\r" + (visible ? text : new String(' ', text.Length)));
            System.Threading.Thread.Sleep(delay);
            visible = !visible;
        }
    }
    

    I also think that the WriteBlinkingText method should be self-contained, so the loop is inside here, but that's just a matter of personal taste I guess :)

提交回复
热议问题