0

When I click a button before it runs the whole method attached to itself, I want the button to reset the whole content. So if i Use Application.Restart(); it would just restart the application but does not run the rest. How will I be able to solve this problem? The reason why I want to restart the application is there are so many variables and I want to reset all of them. This reseting process has to be done by clicking a particular button. However that button has heaps of functions, running the Application.Restart(); method at anywhere on this method. causes this method not to execute. So basically I want this method to restart all variables and then run the rest(starting from enableButtons(); )

private void puzzleToolStripMenuItem_Click(object sender, EventArgs e) {
        Application.Restart();
        enableButtons();
        puzzleDataMethod();
        MessageBox.Show...
B.K.
  • 9,982
  • 10
  • 73
  • 105
Sarp Kaya
  • 3,686
  • 21
  • 64
  • 103
  • 2
    Please explain why you restarting app? – Renatas M. May 30 '12 at 11:03
  • Better explain what do you mean by "When I click a button before it runs the whole method attached to itself" – Shai May 30 '12 at 11:04
  • @Renuiz and Shai explained more – Sarp Kaya May 30 '12 at 11:13
  • 2
    *The reason why I want to restart the application is there are so many variables and I want to reset all of them.* and you have verified that your application is suffering memory leaks? isn't this rather an issue in the design when the application needs this amount of memory consequently? what will ensure that you will not be suffering the very same reason for this memory usage? –  May 16 '15 at 22:07

4 Answers4

2

You should move all of your variables into one container. You will reinitialize it when application needs restart.

Viacheslav Smityukh
  • 5,652
  • 4
  • 24
  • 42
  • I can't do that as this is application needs to be done accordingly to the UML design given to me. – Sarp Kaya May 30 '12 at 11:19
  • 2
    If you can't do it for any reason. You should reset all of your variables manually. I'm sure that a Application rest it's a more dangerous parctice then step around UML design. – Viacheslav Smityukh May 30 '12 at 11:23
  • why don't you suggest adaptions to the design when there's a potential flaw or enhancement...? –  May 16 '15 at 22:14
0

You can't really do this. Your only course of action is to persist to disk any state you want brought back in after the restart and make sure Application.Restart() is the last call in your method.

It might be as simple as setting a true/false flag in a file in isolated storage that tells the new instance of your app that it's starting as a result of a restart, so it can run those commands you currently are trying to run after your call to Application.Restart().

Neil Barnwell
  • 41,080
  • 29
  • 148
  • 220
0

If i understand right. You want to

  1. kill old form.
  2. open new form.
  3. run fuctions on new form.

You do it like this:

public puzzleForm  
{ 

 void puzzleForm_Load(){    }
     
 void btn_ReOpen_click()
 {   
    Helper.NewPuzzle_Show();
   
    //Close old form.
    this.Close(); 
 }
 
 public void myResetFuncs()
 {
     enableButtons();
     puzzleDataMethod();
     MessageBox.Show...
 }

}

Create new class named "Helper.cs" copy paste this in it.

public static class Helper
{
  public static void NewPuzzle_Show()
  {
    // open new form
    var newform = new puzzleForm();
    newform.Show();
   
    //run Important funcs
    newform.myResetFuncs();
  }
}

bh_earth0
  • 2,537
  • 22
  • 24
0

If you want data to be cleared you should include logic to do so, and not play around with process exit, etc.

The right thing to do, to make it clear what is going on, and a future version of yourself not to hit their head in the wall is to have a clearly marked function that does what you want. Being lazy isn't the smart thing to do in this case IMHO.

class Form1 : Form
{
    void ClearData()
    {
        // code that resets the data to defaults.
    }
}
John Alexiou
  • 28,472
  • 11
  • 77
  • 133