2

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?

Doug T.
  • 64,223
  • 27
  • 138
  • 202
ale
  • 3,301
  • 10
  • 40
  • 48
  • you might want to edit your post, and format your code with the Code Sample button. It's the one with the two curly braces { } – Charles Lambert Apr 18 '11 at 18:12
  • Why post the similar question twice? http://stackoverflow.com/questions/5707011/how-call-the-main-method-or-restar-the-app-console – Chandu Apr 18 '11 at 18:13
  • In this part I need restart the console app, for example: Application.Restart(), or Main(string[] args); or something like that – ale Apr 18 '11 at 18:15
  • because I need to explain better. sorry.. – ale Apr 18 '11 at 18:19

7 Answers7

3

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.

asawyer
  • 17,642
  • 8
  • 59
  • 87
  • 1
    that will re-executre the startup code, sure... but that's far from 'restarting' the app – Robert Levy Apr 18 '11 at 18:17
  • @Robert Sure, but the OP is not making it very clear exactly what he's trying to do. In his previous question we demonstrated how to kill and restart the exe programatically so at this point I decided to go dirt simple and see if he is just over thinking it maybe? – asawyer Apr 18 '11 at 18:19
  • ok guys I'm trying to do somethig like this: Console.restart() or Application.restart() or Program.Main(string[] args)... It's this like me sense? – ale Apr 18 '11 at 18:25
  • Nothing is stopping you from calling the main method again with whatever parameters you want. What are you trying to *accomplish* here? If you just need to reinit the program to a startup state, just write a method that does just that. If you need to physically stop and create a new proccess, look at the last question's responses. – asawyer Apr 18 '11 at 18:33
  • can I see the method that you say? – ale Apr 18 '11 at 19:09
  • @ale Not without seeing your entire source code. You are the one who knows what your program needs to do on startup, not me. – asawyer Apr 18 '11 at 19:33
2
if(restart.ToUpper() == "R") { 
    Close();
    System.Diagnostics.Process.Start(Application.ExecutablePath);
}
Priyank
  • 10,503
  • 2
  • 27
  • 25
0

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!

Omar Moodie
  • 263
  • 3
  • 13
0
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);
            }
Pran
  • 1,817
  • 23
  • 23
0

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...

Declan Nnadozie
  • 1,805
  • 1
  • 10
  • 20
0
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 */);
}
Charles Lambert
  • 5,042
  • 26
  • 47
-1

New to C#, I apologize for mistakes. Use this:

    static void Restart() {
    String[] n = new String[10];
                    n[0] = "hi";
                    Main(n);
    }                                  

Main takes a string array for reasons I am unaware of. Seems useless to me, because I tried removing it and nothing seemed to change. So, maybe this works too: Try removing the string[] args part of Main() so you can just type Main(); without issues.

Alex_Sour
  • 29
  • 3
  • He ask how to call main function or restart the app,.... I dont know what String array is doing here.... as for the "Main(n)" are you recursively calling main or calling new method "Main" Show a working code – Declan Nnadozie Jun 25 '19 at 13:41
  • Yes, he asked to call the Main method or restart the code. It calls the main method, which takes a string array. By calling Main, it restarts the code. The code works, and you should correct your grammar. “He ask” “Show a working code” – Alex_Sour Jun 26 '19 at 14:05
  • Am not checking if the code could run, am saying a code the OP can copy-n-paste into his working app, thats what I mean by a working code.....(thanks for Korecting me) – Declan Nnadozie Jun 26 '19 at 14:53
  • You can copy and paste it. It works anywhere. Also, more mistakes: "Am not" "am" "a working code" "Korecting" – Alex_Sour Jun 30 '19 at 01:57
  • I would like to have a constructive argument with you, because this code is a recursive approach... means any error that happens, it call 'restart' function, which in turn calls main..... while main function its created on top of the former main(the stack)....... in C/C++ if a Main is a Big function you will get a segmentation fault..... Pray you dont have much conditions that warrant restart the app... Give me your view---- Mister Englist-Teacher-CSharp-Programmer.. :) – Declan Nnadozie Jun 30 '19 at 19:09