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. WPF
  4. How to make Single instance application to run

How to make Single instance application to run

Scheduled Pinned Locked Moved WPF
csharpwpftutorialquestion
7 Posts 4 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.
  • J Offline
    J Offline
    Joe Rozario
    wrote on last edited by
    #1

    Dear friends, I have wpf application i could double click on the exe and N number of instance, but i want only one application to run. how to do that? any idea? suggestion? Thank You Joe.I

    N P B 3 Replies Last reply
    0
    • J Joe Rozario

      Dear friends, I have wpf application i could double click on the exe and N number of instance, but i want only one application to run. how to do that? any idea? suggestion? Thank You Joe.I

      N Offline
      N Offline
      Not Active
      wrote on last edited by
      #2

      Google is your friend http://www.nathanm.com/csharp-wpf-singleton-application/[^]


      I know the language. I've read a book. - _Madmatt

      1 Reply Last reply
      0
      • J Joe Rozario

        Dear friends, I have wpf application i could double click on the exe and N number of instance, but i want only one application to run. how to do that? any idea? suggestion? Thank You Joe.I

        P Offline
        P Offline
        Pete OHanlon
        wrote on last edited by
        #3

        You have to take over the creation of the main form yourself, rather than using the StartupUri. A common way to do this is, in the OnStartup, detect whether or not another instance of the application is running by using a system wide mutex. Here's an example that does a basic level of checking:

        private Mutex _mutex;
        private override OnStartup(StartupEventArgs e)
        {
        _mutex = new Mutex(false, @"Global\\MyApplicationIdentifier");
        if (_mutex.WaitOne(0, false))
        {
        base.OnStartup(e);
        }
        else
        {
        Application.Current.Shutdown();
        }
        }

        "WPF has many lovers. It's a veritable porn star!" - Josh Smith

        As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

        My blog | My articles | MoXAML PowerToys | Onyx

        J 1 Reply Last reply
        0
        • J Joe Rozario

          Dear friends, I have wpf application i could double click on the exe and N number of instance, but i want only one application to run. how to do that? any idea? suggestion? Thank You Joe.I

          B Offline
          B Offline
          BechBej
          wrote on last edited by
          #4

          you have to implement the singleton pattern in this case move the startupuri attribute form the app.xaml and make the constructor of the statup window as private then define a static method within the form and instanciate that form within that static method private LunchWindow() { InitializeComponent(); } static public LunchWindow Singleton() { return new LunchWindow(); } override the OnStartup method of the app.cs and call that static method within the scope of the on startup method public partial class App : Application { protected override void OnStartup(StartupEventArgs e) { LunchWindow singleInstance = LunchWindow.Singleton(); singleInstance.Show(); } }

          P 1 Reply Last reply
          0
          • P Pete OHanlon

            You have to take over the creation of the main form yourself, rather than using the StartupUri. A common way to do this is, in the OnStartup, detect whether or not another instance of the application is running by using a system wide mutex. Here's an example that does a basic level of checking:

            private Mutex _mutex;
            private override OnStartup(StartupEventArgs e)
            {
            _mutex = new Mutex(false, @"Global\\MyApplicationIdentifier");
            if (_mutex.WaitOne(0, false))
            {
            base.OnStartup(e);
            }
            else
            {
            Application.Current.Shutdown();
            }
            }

            "WPF has many lovers. It's a veritable porn star!" - Josh Smith

            As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

            My blog | My articles | MoXAML PowerToys | Onyx

            J Offline
            J Offline
            Joe Rozario
            wrote on last edited by
            #5

            Thanks Pete O'Hanlon, It works fine. Thank you by Joe

            P 1 Reply Last reply
            0
            • B BechBej

              you have to implement the singleton pattern in this case move the startupuri attribute form the app.xaml and make the constructor of the statup window as private then define a static method within the form and instanciate that form within that static method private LunchWindow() { InitializeComponent(); } static public LunchWindow Singleton() { return new LunchWindow(); } override the OnStartup method of the app.cs and call that static method within the scope of the on startup method public partial class App : Application { protected override void OnStartup(StartupEventArgs e) { LunchWindow singleInstance = LunchWindow.Singleton(); singleInstance.Show(); } }

              P Offline
              P Offline
              Pete OHanlon
              wrote on last edited by
              #6

              BechBej wrote:

              you have to implement the singleton pattern in this case

              Errm, no you don't. Unless I am missing something really basic here, a singleton has no effect in this case because the singleton is per application domain. The generally accepted way to do this is to use a Mutex to determine whether or not there is an instance of the application currently running.

              "WPF has many lovers. It's a veritable porn star!" - Josh Smith

              As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

              My blog | My articles | MoXAML PowerToys | Onyx

              1 Reply Last reply
              0
              • J Joe Rozario

                Thanks Pete O'Hanlon, It works fine. Thank you by Joe

                P Offline
                P Offline
                Pete OHanlon
                wrote on last edited by
                #7

                Joe - you're welcome. Glad to help.

                "WPF has many lovers. It's a veritable porn star!" - Josh Smith

                As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

                My blog | My articles | MoXAML PowerToys | Onyx

                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