Petr
Petr

Reputation: 569

C# console app - explicit termination (not Env..Exit)

I have heard that on .NET CF Environment.Exit does not work. Is there any universal way how to terminate (in a standard way) the console app? Thank you

Upvotes: 1

Views: 1022

Answers (1)

dtb
dtb

Reputation: 217401

An application automatically terminates when there is no non-background thread running.

A thread automatically stops running when there is no more code to execute.

So, just make your application have no more code to execute when you want it to terminate.

class Program
{
    static void Main()
    {
        ConsoleKeyInfo cki;

        do
        {
            Console.Write("Press a key: ");
            cki = Console.ReadKey(true);
            Console.WriteLine();

            if (cki.Key == ConsoleKey.D1)
            {
                Console.Write("You pressed: 1");
            }
        }
        while (cki.Key != ConsoleKey.D2);

    } // <-- application terminates here
}

Upvotes: 1

Related Questions