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?
Asked
Active
Viewed 1,148 times
1
-
Maybe by using Split method on any given string – Daniel B Jul 16 '18 at 21:59
-
share the sample code, you have tried – Vinit Jul 16 '18 at 21:59
-
Can you show the part of your code which writes the text? – Ivan Verges Jul 16 '18 at 21:59
-
Check this https://stackoverflow.com/questions/25337336/how-to-make-text-be-typed-out-in-console-application – Hitesh Anshani Jul 16 '18 at 22:02
-
@D-johnAnshani - yes, that's a better duplicate – hatchet - done with SOverflow Jul 16 '18 at 22:03
1 Answers
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