Reputation: 3431
When an exception occurs I want to restart all the processing or start the Main method, after this other method:
public void DisplayMessage(string message) {
Console.WriteLine(message, "Rebuild Log Files");
Console.WriteLine(" Press Enter to finish, or R to restar the program...");
string restart = Console.ReadLine();
if(restart.ToUpper() == "R") {
//Call the Main method or restart the app
}
Console.ReadKey();
}
Note: the main method contains some user written data.
How can I do this?
Upvotes: 2
Views: 21483
Reputation: 2057
I think your design approach should change if what your app to autorestart.... here's a Pseudocode
main (){
errorObj = null;
internalFunct(errorObj);
if(errorObj != null) return;
secondINternalFunction();
}
Running the app...
while(!errorObj){
main();
}
Why i prefer this approach, because Main, or function recall is avoided... If your working with small memory, there is not point recalling main on the stack of limited space... you have no choice but to be iterative...
Upvotes: 0
Reputation: 1906
DialogResult result = MessageBox.Show("Do You Really Want To Logout/Exit?", "Confirmation!", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
this.Close();
System.Diagnostics.Process.Start(Application.ExecutablePath);
}
Upvotes: 0
Reputation: 290
I actually just got done with this problem earlier by the time you read this post. I kind of duplicated the ORIGINAL main method, changed a few options on the original, and left the copy to call the new main method. Here is what I mean.
static void Main(string[] args)
{
Program CallingTheRealMain = new Program();
CallingTheRealMain.Main2();
}
public void Main2()
{
//Any code here
}
I originally needed to do this because I needed to loop back to the main method but couldn't because it was static. This code worked fine for me, hope it does for you too if you choose to implement it. Hope I helped. Happy coding!
Upvotes: 0
Reputation: 10623
if(restart.ToUpper() == "R") {
Close();
System.Diagnostics.Process.Start(Application.ExecutablePath);
}
Upvotes: 2
Reputation: 5132
static void Main(string[] args) {
try {
// code here
} catch /* or finally */ {
DisplayMessage(/* pass in any state from Main() here */);
}
}
static void DisplayMessage(/* passed in state from Main() */) {
// original DisplayMessage() code
// if R was pressed
Main(/* put any args to pass to Main() in here */);
}
Upvotes: 0
Reputation: 17808
Ok you have a main
void main(...)
{
some code
}
All you need to do is...
void main()
{
runStartUpCode();
}
void runStartUpCode()
{
some code
}
Then when you need to restart the code, call runStartUpCode() again.
Upvotes: 3