Self-restarting application

  • Thread starter Thread starter GTi
  • Start date Start date
G

GTi

Does anyone have any good code for a self restarting application?
Sometimes I need to exit my application and start it again, what is the
best method of doing this in a windows form application.
The reason why I want to this is complicated, so a restart is faster
and more easier whey of doing it.
 
Hi,

Marc Gravell said:
System.Windows.Forms.Application.Restart();

Note that this only works in 2.0 , in 1.X there is no Restart() method
 
Hi,

GTi said:
Does anyone have any good code for a self restarting application?
Sometimes I need to exit my application and start it again, what is the
best method of doing this in a windows form application.
The reason why I want to this is complicated, so a restart is faster
and more easier whey of doing it.

If you are using 2.0 you have a Restart method. if you are in 1.X you could
use a workaround, like spawning a new process (the spawned .exe can be
embedded in your original one) , you pass as a parameter the path to your
executable and simply exit the app, the spawned process is now still running
and it does simply wait until your original finished, and create the same
process again ( using the parameter passed before ) . It may sounds complex
but it's very easy really
 
Marc said:
System.Windows.Forms.Application.Restart();

Marc

Hi hi - how thoughtful by MS.

Next question.
Can I put this anywhere in my code - just where I detect when I should
restart ?
Not in any FormClosing metods ?
 
Technically - probably

In reality, you'd be wise to bring things to a logical conclusion first,
particularly if you have other threads playing with unmanaged resources etc;
I'm not sure how aggressive this method is in terms of aborting threads etc,
and I wouldn't want my lazy-disk-writing to get killed in the middle of a
file for an everyday reason.

Myself, I'd be paranoid and close down everything cleanly, and essentially
use this as a last line in my Main() - i.e. something like:

static void Main() {
bool restart;
using(SomeForm form = new SomeForm()) {
form.ShowDialog();
restart = form.RestartApplication;
}
if(restart)
Application.Restart();
}

You might get away with calling it elsewhere, but on your own head ;-p

Marc
 
Marc said:
Technically - probably

In reality, you'd be wise to bring things to a logical conclusion first,
particularly if you have other threads playing with unmanaged resources etc;
I'm not sure how aggressive this method is in terms of aborting threads etc,
and I wouldn't want my lazy-disk-writing to get killed in the middle of a
file for an everyday reason.

Myself, I'd be paranoid and close down everything cleanly, and essentially
use this as a last line in my Main() - i.e. something like:

static void Main() {
bool restart;
using(SomeForm form = new SomeForm()) {
form.ShowDialog();
restart = form.RestartApplication;
}
if(restart)
Application.Restart();
}

You might get away with calling it elsewhere, but on your own head ;-p

Marc

OK - It will just terminate the program where the line is.
It will not call any FormClosing metods.
Nice to know:
Then I just use a bool variables to indicate if it need a Restart at
the end of program.
Tx Marc
 
Hello GTi,

One of the solutions: you can build all main logic in separate DLL. Main
app is a simple controlling EXE that load dependent DLL in different ApplicationDomains.
If u need to restart app - just reload appdomain. This way, for example,
is used in SQLServer to keep in reliable.

Take into account that this solution has disadvantages - inter-appdomains
calls are expensive. You need carefully design you app in this case

G> Does anyone have any good code for a self restarting application?
G> Sometimes I need to exit my application and start it again, what is
G> the
G> best method of doing this in a windows form application.
G> The reason why I want to this is complicated, so a restart is faster
G> and more easier whey of doing it.
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/members/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top