'How to restart application without exiting and reloading?
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...
Solution 1:[1]
You should move all of your variables into one container. You will reinitialize it when application needs restart.
Solution 2:[2]
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().
Solution 3:[3]
If i understand right. You want to
- kill old form.
- open new form.
- 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();
}
}
Solution 4:[4]
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.
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Viacheslav Smityukh |
| Solution 2 | Neil Barnwell |
| Solution 3 | |
| Solution 4 | John Alexiou |
