The documentation comes from the Markdown files in the source code, so is always up-to-date but available only in English. Enjoy!
Small class that contains some tools for Console
.
Just an object that you can use as lock to write consecutive blocks of text in the console in multi-threaded console applications.
public static readonly object SyncKey = new object();
Writes in the console using the same like than the previous call to WriteSameLine
. Useful for progress indicators in long running console processes.
public static void WriteSameLine(string str)
public static void WriteSameLine(string format, params object[] parameters)
public static void ClearSameLine()
Writes a line in the console using the specified color. The method saves and restores the current color.
public static void WriteColor(ConsoleColor color, string str)
public static void WriteColor(ConsoleColor color, string format, params object[] parameters)
public static void WriteLineColor(ConsoleColor color, string format, params object[] parameters)
public static void WriteLineColor(ConsoleColor color, string str)
One-liners for asking the user for information in console applications:
//Asks the user a yes/no question
public static bool Ask(string question)
//Asks the user a yes/no question that can be remembered by writting !
public static bool Ask(ref bool? rememberedAnswer, string question)
//Asks the user a string question
public static string Ask(string question, params string[] answers)
//Asks the user a string question that can be remembered by writting !
public static string Ask(ref string rememberedAnswer, string question, params string[] answers)
Asks the user to choose one of the different values with a ConsoleSwitch format:
//Asks the user a yes/no question
public static string AskSwitch(string question, List<string> options)
This series of methods are usefull to give some 'working in progresss' indicator for long-runnning operations that do not provide progress information. Tipically database operations like long queries and updates.
The method counts the number of seconds since the operations was triggered off. The counter runs in a different thread and your code runs in the current one.
//Basic variation, just the spinning counter
public static void WaitExecute(Action action)
//Also writes starting text to give some hint of the current task being done
public static void WaitExecute(string startingText, Action action)
//Writes the text in yellor and returns the falue returned in the function, usefull for long running queries
public static T WaitQuery<T>(string startingText, Func<T> query)
//Writes the text in gray and the number of affected rows when the function is finished. Usefull for long UnsafeInsert/UnsafeDelete/UnsafeUpdate
public static void WaitRows(string startingText, Func<int> updateOrDelete)
Example
SafeConsole.WaitRows("Removing all exceptions", ()=>Database.Query<ExceptonEntity>().UnsafeDelete());
© Signum Software. All Rights Reserved.
Powered by Signum Framework