I need to restart the app Console when the user press "R".
I have this
Console.WriteLine(message, "Rebuild Log Files"
+ " Press Enter to finish, or R to restar the program...");
string restar = Console.ReadLine();
if(restar.ToUpper() == "R")
{
//here the code to restart the console...
}
thanks
Exit a Console Application With the return Method in C# The return statement ends the execution of a method and returns the control to the calling or the main method. We can use the return statement inside the main() function to end our console application's execution.
The easiest way to restart an application in C# is to use the Application. Restart() function. The Application. Restart() function is used to restart an application in C#.
Techopedia Explains Console ApplicationThe program structure of a console application facilitates a sequential execution flow between statements. Designed for the keyboard and display screen, a console application is driven by keyboard and system events generated by network connections and objects.
// Starts a new instance of the program itself
System.Diagnostics.Process.Start(Application.ExecutablePath);
// Closes the current process
Environment.Exit(0);
I realize that this is 7 years old, but I just came across this. I think actually calling the executable and closing the current program is a bit of a cluge. As stated before, this is being over thought. I think that the cleanest and most modular way is to take everything that is in the Main
method and make a different method, let's say Run()
that contains everything that was in the Main
method and then call the new Run()
method from the Main
method or wherever in the code it is desired to re-start the program.
So if the Main
method looked like this:
static void Main(string[] args)
{
/*
Main Method Variable declarations;
Main Method Method calls;
Whatever else in the Main Method;
*/
int SomeNumber = 0;
string SomeString = default(string);
SomeMethodCall();
//etc.
}
Then just create a Run()
method and put everything from Main
into it, like so:
public static void Run()
{
//Everything that was in the Main method previously
/*
Main Method Variable declarations;
Main Method Method calls;
Whatever else in the Main Method;
*/
int SomeNumber = 0;
string SomeString = default(string);
SomeMethodCall();
//etc.
}
Now that the Run()
method is created and it has all the stuff that was in the Main
method before, just make your main method like so:
static void Main(string[] args)
{
Run();
}
Now, wherever in the code you want to "re-start" the program, just call the Run()
method like so:
if(/*whatever condition is met*/)
{
//do something first
//then "re-start" the program by calling Run();
Run();
}
So this is a look at the whole program simplified:
EDIT: When I posted this originally I didn't take into consideration any arguments that might have been passed to the program. To account for this four things need to be changed in my original answer.
declare a global List<string>
like this:
public static List<string> MainMethodArgs = new List<string>();
.
In the Main
method set the value of the MainMethodArgs
list equal to the
values passed into the Main
method via args
like this:
MainMethodArgs = args.ToList();
When creating the Run()
method change the signature so that it expects a
string[]
called args
to be passed to it like this:
public static void Run(string[] args)
{
....
}
Wherever in the program the Run()
method is called, pass MainMethodArgs
to Run()
like this:
Run(MainMethodArgs.ToArray());
I changed the example below to reflect these changes.
using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ExampleConsole
{
class Program
{
public static List<string> MainMethodArgs = new List<string>();
static void Main(string[] args)
{
MainMethodArgs = args.ToList();
Run(MainMethodArgs.ToArray());
}
public static void Run(string[] args)
{
Console.WriteLine("Run() is starting");
Console.ReadLine();
//stuff that used to be in the public method
int MainMethodSomeNumber = 0;
string MainMethodSomeString = default(string);
SomeMethod();
//etc.
}
public static void SomeMethod()
{
Console.WriteLine("Rebuild Log Files"
+ " Press Enter to finish, or R to restar the program...");
string restar = Console.ReadLine();
if (restar.ToUpper() == "R")
{
//here the code to restart the console...
Run(MainMethodArgs.ToArray());
}
}
}
}
In effect the program is "re-started" without having to re-run the executable and close the existing instance of the program. This seems a lot more "programmer like" to me.
Enjoy.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With