Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Detect if Application.Restart Fails.

Detect if Application.Restart Fails.

Scheduled Pinned Locked Moved C#
question
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kevinnicol
    wrote on last edited by
    #1

    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?

    A 1 Reply Last reply
    0
    • K kevinnicol

      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?

      A Offline
      A Offline
      Alan N
      wrote on last edited by
      #2

      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.

      K 1 Reply Last reply
      0
      • A Alan N

        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.

        K Offline
        K Offline
        kevinnicol
        wrote on last edited by
        #3

        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.

        A 1 Reply Last reply
        0
        • K kevinnicol

          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.

          A Offline
          A Offline
          Alan N
          wrote on last edited by
          #4

          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 the Main() 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

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups