Restarting Application on FormClosed event in C#
I have designed a form and on FormClosed event I am restar开发者_开发知识库ting the Application by Application.Restart();
Now question is
- Is the application restarting on same thread or on a new thread?
- How can I Close the application when my application is already running?
The restarted application is a new process, so it is also a completely new thread.
To close use; Application.Exit()
To Kill: Process.GetCurrentProcess().Kill();
EDIT - Added Kill!
The application is restarted on a new thread.
I would work with the FormClosing Event
and set the FormClosingEventArgs.Cancel = true
when I want the application not to actually be closed, and let it go when it has to be closed.
public void frmMyForm_FormClosing(object sender, FormClosingEventArgs e) {
if ("frmMyForm has to be closed and the application closed")
e.Cancel = true;
// else the e.Cancel will never be changed, then the application is closed normally.
}
The FormClosing Event is called when you make a call to frmMyForm.Close()
.
Hope I understood the question right.
@Javed i think the best choice will be start new instance of your application using Process while closing and give your first instance to die in peace;)
The current application will be Closed/Shutdown, and a new process (with the same startup arguments) will be created.
- So this will be in a new thread. A new process even.
- The current application will be closed by calling restart. The new application has to take care of its own closing.
You can prevent the closing of the current application, by cancelling the close in a FormClosing event (from an open form). Even if you can cancelling the shutdown, the new appliction will be started, and you will end up by having two instances of your application active.
精彩评论