tags:

views:

119

answers:

6

I am trying to write on Console let's say "Enter your User Name:" and what I know is to use Console.WriteLine("Enter your...");

But I want this message of prompt appears as its' being typed like Aliens or star trek computers. Your expert answer with best practices is much appreciated. Thanks

+9  A: 
    public static void WriteSlow(string txt) {
        foreach (char ch in txt) {
            Console.Write(ch);
            System.Threading.Thread.Sleep(50);
        }
    }
Hans Passant
Beat me to it :( :(
Aaron
@Steve Guidi: And... how would that work since Console is a static class?
MikeP
It should be an extension method on TextWriter instead.
plinth
Hmm, does this mean computers in/from the future run on 1 kHz clocks?
Henk Holterman
You should add a click sound in there for each character to enhance the effect.
Chris Taylor
@Chris yes that is what now I am trying to do :). I think Beep class would work?
Zai
+1  A: 
 foreach (var character in "Enter your...")
        {
            Console.Write(item);
            System.Threading.Thread.Sleep(300);

        }
mumtaz
+1  A: 

Just use Thread.Sleep in the System.Threading namespace to add a wait between each character.

String text = "Enter your username";
foreach (char c in text)
{
    Console.Write(c);
    System.Threading.Thread.Sleep(100);
 }
Hugo Migneron
+1  A: 

You could create a loop over your text, sleeping a small amount of time between letters like:

string text = "Enter your User Name:";
for(int i = 0; i < text.Length; i++)
{
    Console.Write(text[i]);
    System.Threading.Thread.Sleep(50);
}
Ezweb
+5  A: 

I think using Random to sleep thread makes for a nice touch.

    private static void RetroConsoleWriteLine()
    {
        const string message = "Enter your user name...";
        var r = new Random();
        foreach (var c in message)
        {
            Console.Write(c);
            System.Threading.Thread.Sleep(r.Next(50,300));
        }
        Console.ReadLine();
    }

Or, if just for the hell of it and to stand out from the rest

    private static void RetroConsoleWriteLine()
    {
        const string message = "Enter your user name...";
        var r = new Random();
        Action<char> action = c =>
        {
            Console.Write(c);
            System.Threading.Thread.Sleep(r.Next(50, 300));
        };
        message.ToList().ForEach(action);
        Console.ReadLine();
    }
danijels
+1  A: 

My only addition would be a little bit of randomness (starting with Hans' answer):

public static void WriteSlow(string txt) 
{ 
    Random r = new Random();
    foreach (char ch in txt) 
    { 
        Console.Write(ch); 
        System.Threading.Thread.Sleep(r.Next(10,100)); 
    } 
} 
wageoghe