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. How to check if only 1 instance of an application is running?

How to check if only 1 instance of an application is running?

Scheduled Pinned Locked Moved C#
tutorialquestion
7 Posts 3 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.
  • B Offline
    B Offline
    bouli
    wrote on last edited by
    #1

    Hello gurus, I'd like to know how can see if my application already runs or not? Only 1 instance of my application can run. How to do that? Best regards. Fred.

    There is no spoon.

    K 1 Reply Last reply
    0
    • B bouli

      Hello gurus, I'd like to know how can see if my application already runs or not? Only 1 instance of my application can run. How to do that? Best regards. Fred.

      There is no spoon.

      K Offline
      K Offline
      KeithF
      wrote on last edited by
      #2

      Hi You could use this method: Use the following code on you load or main method if (PrevInstance()) { MessageBox.Show("The program is already running"); Application.Exit(); } This is the method called from your main code private static bool PrevInstance() { if (Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName).Length > 1) { return true; } return false; } Hope this helps.

      M B 2 Replies Last reply
      0
      • K KeithF

        Hi You could use this method: Use the following code on you load or main method if (PrevInstance()) { MessageBox.Show("The program is already running"); Application.Exit(); } This is the method called from your main code private static bool PrevInstance() { if (Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName).Length > 1) { return true; } return false; } Hope this helps.

        M Offline
        M Offline
        Martin 0
        wrote on last edited by
        #3

        Hello, Never forget to dispose the instances of the processes. All the best, Martin

        1 Reply Last reply
        0
        • K KeithF

          Hi You could use this method: Use the following code on you load or main method if (PrevInstance()) { MessageBox.Show("The program is already running"); Application.Exit(); } This is the method called from your main code private static bool PrevInstance() { if (Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName).Length > 1) { return true; } return false; } Hope this helps.

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

          ok, it works. And how can I Activate the main window of the already running application??? Fred.

          There is no spoon.

          M 1 Reply Last reply
          0
          • B bouli

            ok, it works. And how can I Activate the main window of the already running application??? Fred.

            There is no spoon.

            M Offline
            M Offline
            Martin 0
            wrote on last edited by
            #5

            Hello, You have to get the "MainWindowHandle" property of the process, and use it in the "ShowWindow" method of "user32.dll". Here is a article where it's discriped: http://www.codeproject.com/useritems/MultipleInstance.asp[^] All the best, Martin

            B 1 Reply Last reply
            0
            • M Martin 0

              Hello, You have to get the "MainWindowHandle" property of the process, and use it in the "ShowWindow" method of "user32.dll". Here is a article where it's discriped: http://www.codeproject.com/useritems/MultipleInstance.asp[^] All the best, Martin

              B Offline
              B Offline
              bouli
              wrote on last edited by
              #6

              ok!! I see how to do that :) Thanks for the help guys :)

              There is no spoon.

              M 1 Reply Last reply
              0
              • B bouli

                ok!! I see how to do that :) Thanks for the help guys :)

                There is no spoon.

                M Offline
                M Offline
                Martin 0
                wrote on last edited by
                #7

                You are wellcome!

                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