1

I am making a simple game and I want the text to be outputted in the console word by word. The only method I could find is using the Thread.Sleep() function but this only works for each line. How would I make it write word byword instead?

tinyTim567
  • 21
  • 4

1 Answers1

4

You can make helper method as following.

using System.Threading.Tasks;
public static async Task WriteSlowly(string word, TimeSpan delay = default) 
{       
    if(delay == default) delay = TimeSpan.FromSeconds(1);

    Console.Write(word);
    await Task.Delay(delay);
}

then use it like that

public class Program {
    public async Task Main() {
        while(true) 
        {
            await WriteSlowly("amazing");
        }
    }
}

and whenever you need new line simply use it like that:

await WriteSlowly(word + Environment.NewLine);

Of course this is the simplest solution with default delay time, but you get the idea hopefully.

kuskmen
  • 3,648
  • 4
  • 27
  • 54
  • 1
    @CamiloTerevinto, I was busy adding real example usage that I forgot about the other one, thank you :) – kuskmen Jul 16 '18 at 22:14