Console.ReadLine Break

I am trying to figure out how I can use Console.ReadLine and a timer. My console program is designed to automatically start a long process, and this process is restarted every 30 seconds after the completion of the previous process. I want to give the user the option to split autorun by typing a command. If I use Console.ReadLine (), then it will wait until the user enters something, whereas I want the program to continue through the loop if nothing is entered for 30 seconds. Any thoughts?

For instance:

RunProcess> Wait 30 seconds for user input. If not: Continue Loop

Thank you so much!

+3
source share
2 answers

. , , . , , - .

: -)

EDIT:

System.Threading.Timer 30 , .

+3

Console.ReadLine(), , Console.KeyAvailable, Console.ReadKey(), .

class Program
{
    static bool done;
    static void Main(string[] args)
    {
        int count = 0;            
        done = false;
        while (!done)
        {
            Thread.Sleep(2000);
            count++;
            Console.WriteLine("Calculation #" + count.ToString());
            if (Console.KeyAvailable)
            {
                ConsoleKeyInfo key = Console.ReadKey();
                if (key.Key == ConsoleKey.Escape)
                {
                    done = true;
                }
            }                                
        }
        Console.WriteLine();
        Console.WriteLine("end");
    }
}
+1

Source: https://habr.com/ru/post/1788314/


All Articles