Open console window

This is how I now open the console window:

private static void Main(string[] args)
{
    // Timers run here

    while(true)
    {
        Console.Read();
    }
}

But he always comes back to me: there must be a better way. The reason it Console.Read()is in the eternal whileis because I do not want my program to terminate, if by mistake I click enterwhile it is working, and I am focused on its window.

Any ideas?

Relevant questions that do not answer my question:

+4
source share
3 answers

, , , . .

- " " . " " ? , ? - :

while (true)
{
    Thread.Sleep(1000);
    DisplayCurrentStats();
}

( , , ), . ( - - , .)

, - . , :

while (true)
{
    Thread.Sleep(1000);
    DisplayCurrentStats();
    var escape = Console.Read();
    if (IsEscapeCharacter(escape))
        break;
}
OutputGoodByeMessage();

IsEscapeCharacter() , , ( ), "escape". ( esc , - , " ".) , .

1 , . , 1/10 ( ), ( 1/10 ):

Thread.Sleep(100);
+4

, , , :

private static void Main(string[] args)
{
    new Thread(new ThreadStart(SomeThreadFunction)).Start();

    while(true)
    {
        Console.Read();
    }
}

static void SomeThreadFunction()
{
  while(true) {
    Console.WriteLine("Tick");
    Thread.Sleep(1000);
  }
}
+4

I do not want my program to end if I mistype enter by mistake

You want something similar to this:

while (Console.ReadKey(true).Key == ConsoleKey.Enter)
{
    Console.Read();
}

The above code does not stop if you press the ENTER key, but it stops working when you press any other keys.

+1
source

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


All Articles