|
/*******************************************************************************
*******************************************************************************
Author: Simon Bridge, June 2011 mailto:srbridge@gmail.com
This code is provided under the Code Project Open Licence (CPOL)
See http://www.codeproject.com/info/cpol10.aspx for details
*******************************************************************************
******************************************************************************/
using System;
using System.Diagnostics;
using System.Threading;
namespace NetworkStatus
{
/// <summary>
/// central point through which to write console messages. options to easily set the color of the text.
/// </summary>
public static class SysConsole
{
private static int threadNo = 0;
/// <summary>
/// if the calling thread has no name specified, assign it one:
/// </summary>
[DebuggerStepThrough]
private static void threadName()
{
if (Thread.CurrentThread.Name == null)
Thread.CurrentThread.Name = "Anon" + (++threadNo).ToString();
}
/// <summary>
/// write a line.
/// </summary>
[DebuggerStepThrough]
public static void WriteLine()
{
Console.WriteLine();
}
/// <summary>
/// write the specified text. will include the thread name and time.
/// </summary>
/// <param name="text"></param>
[DebuggerStepThrough]
public static void Write(String text)
{
WriteLineHeader();
Console.Write(text);
}
/// <summary>
/// write the specified line text.
/// </summary>
/// <param name="text"></param>
[DebuggerStepThrough]
public static void WriteLine(String text, ConsoleColor color)
{
WriteLineHeader();
ConsoleColor cc = Console.ForegroundColor;
Console.ForegroundColor = color;
Console.WriteLine(text);
Console.ForegroundColor = cc;
}
/// <summary>
/// write the specified line text.
/// </summary>
/// <param name="text"></param>
[DebuggerStepThrough]
public static void WriteLine(String text)
{
WriteLineHeader();
Console.WriteLine(text);
}
/// <summary>
/// write the specified text-line.
/// </summary>
/// <param name="text"></param>
/// <param name="fmt"></param>
public static void WriteLine(String text, params Object[] fmt)
{
WriteLineHeader();
Console.WriteLine(text, fmt);
}
/// <summary>
/// append the header to the line about to be written: ThreadName: HH:mm:ss
/// </summary>
[DebuggerStepThrough]
private static void WriteLineHeader()
{
threadName();
ConsoleColor cc = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write(Thread.CurrentThread.Name);
Console.ForegroundColor = ConsoleColor.Gray;
Console.Write(": ");
Console.ForegroundColor = ConsoleColor.Cyan;
Console.Write(DateTime.Now.ToString("HH:mm:ss") + " ");
Console.ForegroundColor = cc;
}
/// <summary>
/// wait for the enter key to be pressed.
/// </summary>
[DebuggerStepThrough]
public static void WaitEnterKey()
{
Console.ReadLine();
}
/// <summary>
/// wait for any key to be pressed.
/// </summary>
[DebuggerStepThrough]
public static void WaitAnyKey()
{
Console.ReadKey(true);
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Wrote his first computer game in Microsoft Basic, on a Dragon 32 at age 7. It wasn't very good.
Has been working as a consultant and developer for the last 15 years,
Discovered C# shortly after it was created, and hasn't looked back.
Feels weird talking about himself in the third person.