I made this class to show to the user that "the program is still running". Like this:
How to use it: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 } }
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.



Reply With Quote![[C#]Spinning Class](http://ragezone.com/hyper728.png)

