Function or Method to Restart an Application on the C#console

Asked

Viewed 576 times

1

Is there any available function or method to Restart the application on the C console#?

static void decision()
    {
        Console.WriteLine("\n Press the home key to recalculate. . .", Console.ForegroundColor = ConsoleColor.Blue);
        Console.ResetColor();

        Console.WriteLine(" Press the esc key to exit. . .", Console.ForegroundColor = ConsoleColor.Magenta);
        Console.ResetColor();

        var option = Console.ReadKey();

        if (option.Key == ConsoleKey.Escape)
        {
            Environment.Exit(0);
        }
        else if (option.Key == ConsoleKey.Home)
        {
            /* reiniciar aplicação console aqui */
        }
        else
        {
            Console.Clear();
            Console.WriteLine("\n Invalid option, try again!", Console.ForegroundColor = ConsoleColor.Red);
            Console.ResetColor();
            decision();
        }
    }
  • What is restarting the application? Better say what you need instead of trying to do something random, you probably don’t need what you’re asking.

2 answers

1

Can be restarted by finishing the current process and creating a new.

As you did not specify which Framework is used, I took the liberty of exemplifying in . NET Core

 class Program
    {
        static bool flag;

        static void Main(string[] args)
        {
            Console.CancelKeyPress += Console_CancelKeyPress;
            while (!flag)
            {
                Console.WriteLine("\n Press the home key to recalculate. . .", Console.ForegroundColor = ConsoleColor.Blue);
                Console.ResetColor();

                Console.WriteLine(" Press the esc key to exit. . .", Console.ForegroundColor = ConsoleColor.Magenta);
                Console.ResetColor();

                var option = Console.ReadKey();

                if (option.Key == ConsoleKey.Escape)
                {
                    flag = true;
                }
                else if (option.Key == ConsoleKey.Home)
                {
                    Process.Start("cmd", $"/c dotnet {Assembly.GetExecutingAssembly().Location}");
                    flag = true;

                }
                else
                {
                    Console.Clear();
                    Console.WriteLine("\n Invalid option, try again!", Console.ForegroundColor = ConsoleColor.Red);
                    Console.ResetColor();
                }
            }

        }

        private static void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e)
        {
            Environment.Exit(0);
        }
    }

The event Console_CancelKeyPress is triggered only when the Ctrl+C keys are pressed.

0

There are some alternatives to get the result you want. The first I can think of is to create a method on the side and call it again

    class Program
    {
        static void Main(string[] args)
        {
            run();
        }
        static void run()
        {
            Console.WriteLine("\n Press the home key to recalculate. . .", Console.ForegroundColor = ConsoleColor.Blue);
            Console.ResetColor();

            Console.WriteLine(" Press the esc key to exit. . .", Console.ForegroundColor = ConsoleColor.Magenta);
            Console.ResetColor();

            var option = Console.ReadKey();

            if (option.Key == ConsoleKey.Escape)
            {
                Environment.Exit(0);
            }
            else if (option.Key == ConsoleKey.Home)
            {
                run();
            }
            else
            {
                Console.Clear();
                Console.WriteLine("\n Invalid option, try again!", Console.ForegroundColor = ConsoleColor.Red);
                Console.ResetColor();

            }
        }
    }

The other would simply put inside a while loop

    class Program
    {
        static void Main(string[] args)
        {
            while (true)
            {

                Console.WriteLine("\n Press the home key to recalculate. . .", Console.ForegroundColor = ConsoleColor.Blue);
                Console.ResetColor();

                Console.WriteLine(" Press the esc key to exit. . .", Console.ForegroundColor = ConsoleColor.Magenta);
                Console.ResetColor();

                var option = Console.ReadKey();

                if (option.Key == ConsoleKey.Escape)
                {
                    break;
                }
                else if (option.Key == ConsoleKey.Home)
                {
                    continue;
                }
                else
                {
                    Console.Clear();
                    Console.WriteLine("\n Invalid option, try again!", Console.ForegroundColor = ConsoleColor.Red);
                    Console.ResetColor();

                }
            }
            Environment.Exit(0);
        }
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.