class Program { static void Main(string[] args) { int y = 0; while (y == 0) { Random RandNum = new Random(); int a = RandNum.Next(10, 101); int b = RandNum.Next(2, 10); int x = a * b; string l = x.ToString(); try { Console.WriteLine("How many will be {0} * {1}:", a, b); int num = Convert.ToInt32(Console.ReadLine()); if (num == x) { Console.WriteLine("Great work, get it some again?"); string answer = Convert.ToString(Console.ReadLine()); if (answer == "Yes" || answer == "yes") { } else { Environment.Exit(0); } } else { Console.WriteLine("No, it wrong"); Console.ReadKey(true); } } catch { Console.WriteLine("Enter number..."); } } } } 

I need that in this moment, after, for example, 5 seconds the message “Need a clue?” Appears

 Console.WriteLine("How many will be {0} * {1}:", a, b); int num = Convert.ToInt32(Console.ReadLine()); 

    1 answer 1

    For this purpose, you can use any timer.

    Add a field to the code:

     static System.Threading.Timer timer = new Timer( n => Console.WriteLine("Need a clue?"), null, Timeout.Infinite, Timeout.Infinite); 

    Here the lambda is a callback (callback function), which will be called periodically. Initially, the parameters are set to Infinite , because the timer should not work yet.

    Further, in the code in the right place we turn on the timer, setting the parameters, after what time it will work the first time and at what intervals it will work later.

     Console.WriteLine("How many will be {0} * {1}:", a, b); timer.Change(5000, 5000); int num = Convert.ToInt32(Console.ReadLine()); timer.Change(Timeout.Infinite, Timeout.Infinite); 

    After receiving a response from the user, we change the timer parameters to Infinite again - it will stop working.

    At the end of your code (before Environment.Exit ) write:

     timer.Dispose(); 

    This will free up resources occupied by the timer. The rule of good tone: you need to clean up after yourself.