All pastes #806038 Raw Edit

Console Loader Indicator

public text v1 · immutable
·published 2007-12-04 22:31 UTC
rendered paste body
public class Loader
    {
        public static char[] LoadChars = new char[] { '|', '/', '-', '\\' };

        public bool IsLoading { get; set; }

        int delay;

        public Loader()
        {
            this.delay = 100;
        }

        public Loader(int delay)
        {
            this.delay = delay;
        }

        public void StopLoader()
        {
            IsLoading = false;
        }

        public void StartLoader()
        {
            Thread thread = new Thread(new ThreadStart(DoLoader));

            IsLoading = true;

            thread.Start();
        }

        private void DoLoader()
        {
            int index = -1;
            int positionX = Console.CursorLeft;
            int positionY = Console.CursorTop;
            Console.CursorVisible = false;

            while (IsLoading)
            {
                index++;
                index %= 4;

                Console.CursorLeft = positionX;
                Console.CursorTop = positionY;
                
                Console.Write(Loader.LoadChars[index]);

                System.Threading.Thread.Sleep(delay);
            }
        }
    }