none
How to make three loading dots in c# RRS feed

  • Question

  • Hi,

    I have a problem with my textbased game in c#. While the user is waiting to press 'enter', I want to have a loop with three loading dots. In the code below I'm able to make the three dots appear one at a time, but struggle to make them disappear and repeat the same thing until the user presses 'enter'. Does anyone have a solution for this?

    Console.Write("Press 'Enter' to continue");
    
                    string dots = "...";
    
                    for (int i = 0; i<dots.Length; i++)
                    { 
                            Console.Write(punkter[i]);
                            System.Threading.
                                Thread.Sleep(50);     
                    }
                    Console.ReadLine();

    Tuesday, January 29, 2019 10:03 PM

All replies

  • Greetings Svetsarn.

    You can make the dots disappear by writing a carriage-return ("\r") to go back to the start of the line, writing three spaces to erase the existing dots, then writing another carriage-return. Or you can get the same effect using Console.SetCursorPosition instead of writing carriage-return.

    Waiting for the user input is more difficult than it sounds. You will probably need to use a timer to write the dots, while the main thread waits for input.

    https://docs.microsoft.com/en-us/dotnet/api/system.threading.timer?view=netframework-4.7.2

    Using Thread.Sleep will not work.

    Tuesday, January 29, 2019 11:07 PM

  •  While the user is waiting to press 'enter', I want to have a loop with three loading dots. In the code below I'm able to make the three dots appear one at a time, but struggle to make them disappear and repeat the same thing until the user presses 'enter'. 

    Do you want something that looks and acts like this?

    using System;
    using System.IO;
    using System.Threading;
    
    namespace CS_show_dots_while_waiting_for_input
    {
        public class ThreadWork
        {
            public static void Dots()
            {
                while (true)
                {                
                    for (int i = 0; i < 3; i++)
                    {
                        Console.Write('.');
                        System.Threading.Thread.Sleep(1000);                    
                        if (i == 2)
                        {
                            Console.Write("\r   \r");
                            i = -1;
                            System.Threading.Thread.Sleep(1000);
                        }
                    }
                }
            }
        }
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("Press 'Enter' to continue");
                Thread thread1 = new Thread(ThreadWork.Dots);
                thread1.Start();
                Console.ReadLine();
                thread1.Abort();
                Console.WriteLine("I'm baaack!");
                Console.ReadLine();
            }
        }
    }
    

    - Wayne


    Wednesday, January 30, 2019 12:12 PM
  • Hi Wayne!

    Yes but not exactly. I would like to have the dots on the same row as the text line so it looks like this:

    "Press 'Enter' to continue..." and also have the dots in the same position when it starts over again.

    So it goes like this:

    1. "Press 'Enter' to continue"

    2. "Press 'Enter' to continue."

    3. "Press 'Enter' to continue.."

    4. "Press 'Enter' to continue..." after this, it starts over again from step 1 and does so until the user presses 'Enter'.

    If the user would write something in the console, I would like the text to not intervene with the loading dots, if it is possible.

    PS, I'm new to c# so every advice is appreciated 

    Thanks!



    • Edited by Svetsarn Wednesday, January 30, 2019 1:23 PM
    Wednesday, January 30, 2019 1:21 PM

  • Yes but not exactly. I would like to have the dots on the same row as the text line so it looks like this:

    "Press 'Enter' to continue..." 

    Putting the dots on the same line as the message is trivial. 
    Make these changes to the code I posted:

    //Console.Write("\r   \r");
    Console.Write("\b\b\b   \b\b\b");
    
    
    //Console.WriteLine("Press 'Enter' to continue");
    Console.Write("Press 'Enter' to continue");
    

    - Wayne

    Wednesday, January 30, 2019 1:36 PM
  • Hi Svetsam,

    Thank you for posting here.

    For your question, please try the code below.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;
    
    namespace ConsoleApp
    {
        public class ConsoleSpinner
        {
            static string[,] sequence = null;
    
            public int Delay { get; set; } = 200;
    
            int totalSequences = 0;
            int counter;
    
            public ConsoleSpinner()
            {
                counter = 0;
                sequence = new string[,] {
                { "/", "-", "\\", "|" },
                { ".", "o", "0", "o" },
                { "+", "x","+","x" },
                { "V", "<", "^", ">" },
                { "   ", ".  ", ".. ", "..." },
                { "=>   ", "==>  ", "===> ", "====>" },
               // ADD YOUR OWN CREATIVE SEQUENCE HERE IF YOU LIKE
            };
    
                totalSequences = sequence.GetLength(0);
            }
    
            /// <summary>
            /// 
            /// </summary>
            /// <param name="sequenceCode"> 0 | 1 | 2 |3 | 4 | 5 </param>
            public void Turn(string displayMsg = "", int sequenceCode = 0)
            {
                counter++;
    
                Thread.Sleep(Delay);
    
                sequenceCode = sequenceCode > totalSequences - 1 ? 0 : sequenceCode;
    
                int counterValue = counter % 4;
    
                string fullMessage = displayMsg + sequence[sequenceCode, counterValue];
                int msglength = fullMessage.Length;
    
                Console.Write(fullMessage);
    
                Console.SetCursorPosition(Console.CursorLeft - msglength, Console.CursorTop);
            }
        }
    
        /// <summary>
        /// Provides keyboard access.
        /// </summary>
        internal static class NativeKeyboard
        {
            /// <summary>
            /// A positional bit flag indicating the part of a key state denoting
            /// key pressed.
            /// </summary>
            private const int KeyPressed = 0x8000;
    
            /// <summary>
            /// Returns a value indicating if a given key is pressed.
            /// </summary>
            /// <param name="key">The key to check.</param>
            /// <returns>
            /// <c>true</c> if the key is pressed, otherwise <c>false</c>.
            /// </returns>
            public static bool IsKeyDown(ConsoleKey key)
            {
                return (GetKeyState((int)key) & KeyPressed) != 0;
            }
    
            /// <summary>
            /// Gets the key state of a key.
            /// </summary>
            /// <param name="key">Virtuak-key code for key.</param>
            /// <returns>The state of the key.</returns>
            [System.Runtime.InteropServices.DllImport("user32.dll")]
            private static extern short GetKeyState(int key);
        }
        class Display_loading_dots
        {
            static void Main(string[] args)
            {
                ConsoleSpinner spinner = new ConsoleSpinner();
                spinner.Delay = 300;
    
                do
                {
                    spinner.Turn(displayMsg: "Press 'Enter' to continue ", sequenceCode: 4);
    
                } while (!NativeKeyboard.IsKeyDown(ConsoleKey.Enter));
    
                Console.Clear();
                Console.WriteLine("Hello");
    
                Console.ReadLine();
                Console.ReadLine();
    
    
            }
    
        }
    }
    
    

    When you press the Enter, break the do while loop. And the clear the console to show hello.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Stanly Fan Friday, February 1, 2019 7:37 AM
    Thursday, January 31, 2019 8:04 AM
    Moderator