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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. active single instance only

active single instance only

Scheduled Pinned Locked Moved C#
tutorialquestion
10 Posts 6 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.
  • R Offline
    R Offline
    reza assar
    wrote on last edited by
    #1

    hi i want to develop an application that , user could not execute more than one instance at a time for example my application name is: "APP" . if some one click APP icon it will be run if there is no another instance running at the current machine what should i do? thanks in advance!

    N A V 3 Replies Last reply
    0
    • R reza assar

      hi i want to develop an application that , user could not execute more than one instance at a time for example my application name is: "APP" . if some one click APP icon it will be run if there is no another instance running at the current machine what should i do? thanks in advance!

      N Offline
      N Offline
      N a v a n e e t h
      wrote on last edited by
      #2

      Take a look at Mutex[^] class.

      Best wishes, Navaneeth

      L 1 Reply Last reply
      0
      • R reza assar

        hi i want to develop an application that , user could not execute more than one instance at a time for example my application name is: "APP" . if some one click APP icon it will be run if there is no another instance running at the current machine what should i do? thanks in advance!

        A Offline
        A Offline
        anishkannan
        wrote on last edited by
        #3

        try this code.... this is for just check ur Exe application name is currently running or not [DllImport("user32.dll", EntryPoint = "SetForegroundWindow")] static extern IntPtr SetForegroundWindow(IntPtr hWnd); private static bool AppIsAlreadyRunning() { Process c = Process.GetCurrentProcess(); foreach (Process p in Process.GetProcessesByName(c.ProcessName)) { if (p.Id != c.Id) { if (p.MainModule.FileName.ToLower().Equals(c.MainModule.FileName.ToLower())) { SetForegroundWindow(p.MainWindowHandle); return true; } } } return false; }

        1 Reply Last reply
        0
        • N N a v a n e e t h

          Take a look at Mutex[^] class.

          Best wishes, Navaneeth

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          You need to be very careful when you are using globally available mutexes running on limited OS accounts. Maybe finding all processes with the same name like the current will be better and safer solution because doesn't require any special permissions.

          Life is a stage and we are all actors!

          N 1 Reply Last reply
          0
          • L Lost User

            You need to be very careful when you are using globally available mutexes running on limited OS accounts. Maybe finding all processes with the same name like the current will be better and safer solution because doesn't require any special permissions.

            Life is a stage and we are all actors!

            N Offline
            N Offline
            N a v a n e e t h
            wrote on last edited by
            #5

            Hristo Bojilov wrote:

            You need to be very careful when you are using globally available mutexes running on limited OS accounts

            What problems are you seeing?

            Best wishes, Navaneeth

            L 1 Reply Last reply
            0
            • N N a v a n e e t h

              Hristo Bojilov wrote:

              You need to be very careful when you are using globally available mutexes running on limited OS accounts

              What problems are you seeing?

              Best wishes, Navaneeth

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              The method System.Threading.Mutex.OpenExisting could throw UnauthorizedAccessException for sample.Will this method work when the current account doesn't have the desired access and cannot acquire it?I've used this mutexes method for my current project but after discovering this exception is thrown to often when running on limited accounts I had removed it and used just simple scan for the running processes which queries Windows performance counter API.

              Life is a stage and we are all actors!

              L 1 Reply Last reply
              0
              • L Lost User

                The method System.Threading.Mutex.OpenExisting could throw UnauthorizedAccessException for sample.Will this method work when the current account doesn't have the desired access and cannot acquire it?I've used this mutexes method for my current project but after discovering this exception is thrown to often when running on limited accounts I had removed it and used just simple scan for the running processes which queries Windows performance counter API.

                Life is a stage and we are all actors!

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

                Hi, I haven't used the managed mutex class yet, however it seems to me the MutexSecurity parameter in one of the constructors should be the key to solving the problems you've had. Anyway, relying on GetProcessesByName() does not seem right as: 1. when a user renames an EXE, the resulting process would also be getting a different name; 2. I expect the Process methods also can fail when you have insufficient rights. :)

                Luc Pattyn


                I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


                1 Reply Last reply
                0
                • R reza assar

                  hi i want to develop an application that , user could not execute more than one instance at a time for example my application name is: "APP" . if some one click APP icon it will be run if there is no another instance running at the current machine what should i do? thanks in advance!

                  V Offline
                  V Offline
                  vtchris peterson
                  wrote on last edited by
                  #8

                  I've implemented a few ways of doing this, each with various limitations. Then, I came across this solution which works great: http://dotnetperls.com/single-instance-windows-form[^]

                  R 1 Reply Last reply
                  0
                  • V vtchris peterson

                    I've implemented a few ways of doing this, each with various limitations. Then, I came across this solution which works great: http://dotnetperls.com/single-instance-windows-form[^]

                    R Offline
                    R Offline
                    reza assar
                    wrote on last edited by
                    #9

                    thanks for your attention but the link is not refer correctly please to fix it!

                    V 1 Reply Last reply
                    0
                    • R reza assar

                      thanks for your attention but the link is not refer correctly please to fix it!

                      V Offline
                      V Offline
                      vtchris peterson
                      wrote on last edited by
                      #10

                      Link works fine for me. Google C# Single-Instance Windows Form by Sam Allen.

                      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