Javed Akram
Javed Akram

Reputation: 15344

Restarting Application on FormClosed event in C#

I have designed a form and on FormClosed event I am restarting the Application by Application.Restart();

Now question is

  1. Is the application restarting on same thread or on a new thread?
  2. How can I Close the application when my application is already running?

Upvotes: 0

Views: 688

Answers (4)

S.Skov
S.Skov

Reputation: 707

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!

Upvotes: 1

GvS
GvS

Reputation: 52518

The current application will be Closed/Shutdown, and a new process (with the same startup arguments) will be created.

  1. So this will be in a new thread. A new process even.
  2. 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.

Upvotes: 0

Edward83
Edward83

Reputation: 6686

@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;)

Upvotes: 0

Will Marcouiller
Will Marcouiller

Reputation: 24132

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.

Upvotes: 0

Related Questions