[C#]Spinning Class

Results 1 to 2 of 2
  1. #1
    Fuck. SheenBR is offline
    ModeratorRank
    Feb 2008 Join Date
    Jaú, BrazilLocation
    2,433Posts

    [C#]Spinning Class

    I made this class to show to the user that "the program is still running". Like this:


    Code:
    /*
            Copyright © 2012, The Divinity Project
            All rights reserved.
            http://board.thedivinityproject.com
            http://www.ragezone.com
    
    
            This file is part of PTEmu.
    
            PTEmu is free software: you can redistribute it and/or modify
            it under the terms of the GNU General Public License as published by
            the Free Software Foundation, either version 3 of the License, or
            any later version.
    
            PTEmu is distributed in the hope that it will be useful,
            but WITHOUT ANY WARRANTY; without even the implied warranty of
            MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
            GNU General Public License for more details.
    
            You should have received a copy of the GNU General Public License
            along with PTEmu.  If not, see <http://www.gnu.org/licenses/>.
     */
    
    #region Includes
    
    using System;
    using System.Threading;
    
    #endregion
    
    namespace PTEmu.Animation
    {
        public static class Spin
        {
            #region Private Members
    
            static bool active = false;
            static ConsoleColor color;
    
            #endregion
    
            #region Public Methods
    
            public static void Run(int interval, ConsoleColor Color)
            {
                if (active == true) return;
                active = true;
                color = Color;
                ThreadPool.QueueUserWorkItem(new WaitCallback(Spinning), interval);
            }
            public static void Run(int interval)
            {
                if (active == true) return;
                active = true;
                ThreadPool.QueueUserWorkItem(new WaitCallback(Spinning), interval);
            }
            public static void Stop()
            {
                active = false;
                Thread.Sleep(100);
            }
    
            #endregion
    
            #region Private Methods
    
            static void Spinning(object stateInfo)
            {
                var interval = (int)stateInfo;
                var line = "";
                var chars = new char[] { '|', '/', '-', '\\' };
                while (active)
                {
                    for (int i = 0; i < chars.Length; i++)
                    {
                        if (!active) break;
                        var backup = new string('\b', line.Length);
                        Console.Write(backup, (color != ConsoleColor.Black) ? color : ConsoleColor.White);
                        line = string.Format("{0}", chars[i]);
                        Console.Write(line, (color != ConsoleColor.Black) ? color : ConsoleColor.White);
                        Thread.Sleep(interval);
                    }
                }
                var backups = new string('\b', line.Length);
                Console.WriteLine(backups, ConsoleColor.White);
            }
    
            #endregion
    
        }
    }
    How to use it:

    Spin.Start(100); -> the chars(|,/,-) will change in a 100ms interval, with WHITE color.
    Spin.Start(100,ConsoleColor.Cyan), same, but with Cyan Color.

    After executing your method, you need to:

    Spin.Stop();

    You can only run it once. I mean, when the Spin is already started, you cant start another one, unless you stop the previous.
    Last edited by SheenBR; 15-02-12 at 03:05 PM.


  2. #2
    Banned Yamachi is offline
    BannedRank
    Oct 2006 Join Date
    Jolly EnglandLocation
    3,517Posts

    Re: [C#]Spinning Class

    1. Don't use \b... Use SetCursorPosition.
    2. Why on Earth are you using String.Format to convert a char to a string?
    3. There's no need to have a for loop inside a while loop, especially if you want it to exit when you call Stop(). Instead, use a counter, increment it at the end of the while loop body, and get the index of the char like this: var curchar = chars[i % 4];
    4. Making the thread sleep for the entire interval the user has chosen is guaranteed to give you a variable framerate. You have to think about the time it takes to actually write the characters, too. I know it doesn't really matter much in this situation, but it would if you were writing code that needed to be accurate.



Advertisement