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. Run the application once

Run the application once

Scheduled Pinned Locked Moved C#
questionworkspace
9 Posts 7 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.
  • S Offline
    S Offline
    sarang_k
    wrote on last edited by
    #1

    Hi all, I have created one windows setup application,when i installed it on the system if i click two times the application is running two times as it should run once. How can i solve it ? Thanks in advance.

    W T L B 4 Replies Last reply
    0
    • S sarang_k

      Hi all, I have created one windows setup application,when i installed it on the system if i click two times the application is running two times as it should run once. How can i solve it ? Thanks in advance.

      W Offline
      W Offline
      Wayne Gaylard
      wrote on last edited by
      #2

      You should run your app using the Mutex class, something like this:-

      bool isNew = true;
      using (Mutex mutex = new Mutex(true, "yourAppName", out isNew))
      {
      if (isNew)
      {
      Application.Run(new MainForm());
      }
      }

      If the app is aplready running and you would like it to be given focus you need to use a Win32 API method called SetForegroundWindow which I'm sure you could find an instance of on Google. Hope this helps

      When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman

      B 1 Reply Last reply
      0
      • W Wayne Gaylard

        You should run your app using the Mutex class, something like this:-

        bool isNew = true;
        using (Mutex mutex = new Mutex(true, "yourAppName", out isNew))
        {
        if (isNew)
        {
        Application.Run(new MainForm());
        }
        }

        If the app is aplready running and you would like it to be given focus you need to use a Win32 API method called SetForegroundWindow which I'm sure you could find an instance of on Google. Hope this helps

        When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman

        B Offline
        B Offline
        BobJanova
        wrote on last edited by
        #3

        :thumbsup: I didn't know about this. Very handy.

        W 1 Reply Last reply
        0
        • B BobJanova

          :thumbsup: I didn't know about this. Very handy.

          W Offline
          W Offline
          Wayne Gaylard
          wrote on last edited by
          #4

          Thanks!

          When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman

          1 Reply Last reply
          0
          • S sarang_k

            Hi all, I have created one windows setup application,when i installed it on the system if i click two times the application is running two times as it should run once. How can i solve it ? Thanks in advance.

            T Offline
            T Offline
            thatraja
            wrote on last edited by
            #5

            Here 5+ alternatives Run Only One Copy Of Application[^]

            thatraja


            My Dad had a Heart Attack on this day so don't...
            Pompeyboy3 here
            | Nobody remains a virgin, Life screws everyone :sigh:

            1 Reply Last reply
            0
            • S sarang_k

              Hi all, I have created one windows setup application,when i installed it on the system if i click two times the application is running two times as it should run once. How can i solve it ? Thanks in advance.

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              WARNING: all the schemes that have been mentioned to you do not prevent the app from launching a second or third time; all they do is try and make the app terminate pretty soon if it isn't the first instance. :)

              Luc Pattyn [My Articles] Nil Volentibus Arduum

              B 1 Reply Last reply
              0
              • S sarang_k

                Hi all, I have created one windows setup application,when i installed it on the system if i click two times the application is running two times as it should run once. How can i solve it ? Thanks in advance.

                B Offline
                B Offline
                BillWoodruff
                wrote on last edited by
                #7

                CP has many articles on making a "Singleton" app:[^]

                When I consider the brief span of my life, swallowed up in the eternity before and after, the little space which I fill, and even can see, engulfed in the infinite immensity of spaces of which I am ignorant, and which knows me not, I am frightened, and am astonished at being here rather than there; for there is no reason why here rather than there, now rather than then.

                Blaise Pascal

                1 Reply Last reply
                0
                • L Luc Pattyn

                  WARNING: all the schemes that have been mentioned to you do not prevent the app from launching a second or third time; all they do is try and make the app terminate pretty soon if it isn't the first instance. :)

                  Luc Pattyn [My Articles] Nil Volentibus Arduum

                  B Offline
                  B Offline
                  BillWoodruff
                  wrote on last edited by
                  #8

                  Luc Pattyn wrote:

                  all the schemes that have been mentioned to you do not prevent the app from launching a second or third time

                  Even the one from Wayne G. using a Mutex ? thanks, Bill

                  When I consider the brief span of my life, swallowed up in the eternity before and after, the little space which I fill, and even can see, engulfed in the infinite immensity of spaces of which I am ignorant, and which knows me not, I am frightened, and am astonished at being here rather than there; for there is no reason why here rather than there, now rather than then.

                  Blaise Pascal

                  P 1 Reply Last reply
                  0
                  • B BillWoodruff

                    Luc Pattyn wrote:

                    all the schemes that have been mentioned to you do not prevent the app from launching a second or third time

                    Even the one from Wayne G. using a Mutex ? thanks, Bill

                    When I consider the brief span of my life, swallowed up in the eternity before and after, the little space which I fill, and even can see, engulfed in the infinite immensity of spaces of which I am ignorant, and which knows me not, I am frightened, and am astonished at being here rather than there; for there is no reason why here rather than there, now rather than then.

                    Blaise Pascal

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

                    It still has to start to detect that there's another instance running. It's not that they don't stop the appearance of an app not being started again, but there will always be a finite period of time where the second instance has spun up - even if it then terminates.

                    Forgive your enemies - it messes with their heads

                    "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                    My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

                    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