Detect if Application.Restart Fails.
-
I have an app that needs to restart after an option is changed. So I'm calling Application.Restart() but there are a few places where the user can abort the close if he or she chooses to. When this happens the app doesn't close, but a new instance opens. Is there a way to detect when this happens and not start the new instance?
-
I have an app that needs to restart after an option is changed. So I'm calling Application.Restart() but there are a few places where the user can abort the close if he or she chooses to. When this happens the app doesn't close, but a new instance opens. Is there a way to detect when this happens and not start the new instance?
Is the existing instance able to continue reliably once the option has been changed? If so it may be better to prompt the user to choose between "Restart now" and "I'll do it myself later" rather than just forcing the restart upon them. Once Restart has been called it does look like the the second instance can't be stopped. However you can arrange for it to shutdown without creating any windows, i.e. create a so called single instance application. The basic code is this (well one way of doing it)
static class Program {
const String UniqueIdentifier = "{9E6F0AC4-B9A1-45QX-A8CF-72F04E6BDE8F}";
static Boolean firstInstance;
static Mutex mutex = new Mutex(true, UniqueIdentifier, out firstInstance);
[STAThread]
static void Main() {
if (firstInstance) {
Application.Run(new Form1());
} else {
MessageBox.Show("Already running");
}
}
}The first instance creates and takes ownership of the mutex. Subsequent instances open the existing mutex and firstInstance is false. Alan.
-
Is the existing instance able to continue reliably once the option has been changed? If so it may be better to prompt the user to choose between "Restart now" and "I'll do it myself later" rather than just forcing the restart upon them. Once Restart has been called it does look like the the second instance can't be stopped. However you can arrange for it to shutdown without creating any windows, i.e. create a so called single instance application. The basic code is this (well one way of doing it)
static class Program {
const String UniqueIdentifier = "{9E6F0AC4-B9A1-45QX-A8CF-72F04E6BDE8F}";
static Boolean firstInstance;
static Mutex mutex = new Mutex(true, UniqueIdentifier, out firstInstance);
[STAThread]
static void Main() {
if (firstInstance) {
Application.Run(new Form1());
} else {
MessageBox.Show("Already running");
}
}
}The first instance creates and takes ownership of the mutex. Subsequent instances open the existing mutex and firstInstance is false. Alan.
There is a problem with using a mutex and application.restart(). Sometimes the second instance will be created before all of the message loops in the existing app have had time to terminate. So it won't realiably restart. I've been toying around with calling system.exit() and then starting up another process. Apparently you can call other code after a System.exit(). So I'll check for the exit to be aborted and if it isn't, I'll then call Application.Restart(). I'll reply to this thread if I can come up with a reliable solution.
-
There is a problem with using a mutex and application.restart(). Sometimes the second instance will be created before all of the message loops in the existing app have had time to terminate. So it won't realiably restart. I've been toying around with calling system.exit() and then starting up another process. Apparently you can call other code after a System.exit(). So I'll check for the exit to be aborted and if it isn't, I'll then call Application.Restart(). I'll reply to this thread if I can come up with a reliable solution.
Hi, These two solutions may be applicable to your situation. In both the main form sets a flag if the user chooses to allow a restart and
Application.Restart()
is called just before theMain()
method returns.Method1()
is mutex-less and makes the assumption that the first instance will exit before instance two 'gets going' (That's a very vague term!).Method2()
will make a second instance wait for 10 seconds and then give up if the first has not released the mutex.static void Main() {
//Method1();
Method2();
}private static void Method1() {
Form1 f = new Form1();
Application.Run(f);
if (f.RestartRequested()) {
MessageBox.Show("Click to restart");
Application.Restart();
}
}private static void Method2() {
const String UniqueIdentifier = "{8F6F0AC4-B9A1-45fd-A8CF-72F04E6BDE8F}";
Mutex mutex;
Boolean firstInstance;
mutex = new Mutex(true, UniqueIdentifier, out firstInstance);
if (!firstInstance) {
if (!mutex.WaitOne(TimeSpan.FromSeconds(10))) {
MessageBox.Show("2nd instance did not aquire the mutex\n" +
"and cannot start");
return;
}
}
Form1 f = new Form1();
Application.Run(f);
if (f.RestartRequested()) {
Application.Restart();
MessageBox.Show("Click within 10 seconds to start the new instance");
mutex.ReleaseMutex();
}
mutex.Close();
}Alan.
modified on Thursday, March 4, 2010 7:34 PM